Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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.


Dave's Answer:

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 {increment}"
  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. :-)


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Shell Script Programming   (Article 7802, Written by )
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?




Reader Comments To Date: 4

Prune said, on January 21, 2008 7:45 AM:

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

Prune said, on January 21, 2008 7:52 AM:

I just forgot, on some Os you also have the command "jot"

Cheers.

Michal Chruszcz said, on May 20, 2010 9:10 AM:

I created a simple tool in Python mimicking the behaviour of the "classic" seq utility: http://python.pastebin.com/gsCRKZjq

Brice said, on March 29, 2013 5:20 AM:

Thank you, very useful

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.