
How do I step through numeric values in a shell script?Please, gimme a advice how to correctly use loop "for i in `seq 1 10`" for Mac OS X, echo $SHELL->/bin/bash, because when I write simple strip: for i in `seq 1 10`;do echo $i;done it outputs -bash: seq: command not found. That's because Mac OS X doesn't actually have a command called "seq", but fortunately the basic task you want to accomplish is easily done as a shell script, and if you're so inclined, drop it in $HOME/bin, add that to your PATH and name it "seq" and you're good to go: #!/bin/sh
# count - step through numeric values until you get to the max value if [ "$#" -lt 2 ] ; then echo "Usage: count exit 1 fi counter="$1" max="$2" if [ "$#" -eq 2 ] ; then step=1 else step=$3 fi while [ $counter -le $max ] ; do echo $counter counter=$( expr $counter + $step ) done exit 0 This is actually kind of fancy as a shell replacement script because not only does it let you step from any value to any secondary value, but you can also specify the increment, so if you need to count only even numbers for some obscure reason, you could do that. If you really want to experiment with scripting, try writing it so that if the second value is less than the former value that it automatically decrements instead of incrementing. That is, count 1 5 would produce 1 2 3 4 5, but count 10 5 would correctly produce 10 9 8 7 6 5 rather than just produce the first value and then find that the test to see if the current value is greater than the max value succeeds. If you really want to explore, also tweak it so you can do Fibonacci sequences and other complicated increments (a Fibonacci sequence is where each value is the sum of the previous two, e.g., 1 1 2 3 5 8 13 21 34 55...). Of course, if you do explore in that direction it might well indicate that you have too much time on your hands, in which case you would probably enjoy my book Wicked Cool Shell Scripts too. :-)
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 7802)
Tagged: bash, linux, shell scripts, unix Previous: Can I just call my new book "for Dummies"? Next: What does "nuked" mean in the peer-to-peer world? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Another solution (on some OS, like Solaris) is to use a special feature of the shell : echo {1..4} 1 2 3 4 echo {8..4} 8 7 6 5 4 Posted by: Prune at January 21, 2008 7:45 AMI just forgot, on some Os you also have the command "jot" Cheers. Posted by: Prune at January 21, 2008 7:52 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|