Count Instead of Sequence

I use Bash one liners a lot. I think they are an important part of any programmers and sysadmins toolkit. If you can’t write a bash one liner, even a simple iterator, then you really need to learn. I promise it will make your life infinitely more pleasant.

Frequently I find myself writing things that require a loop or an increment of numbers. A good example would be like something that would walk over my web servers and check their uptime, load averages, etc. Using seq, that’s easy. But since Mac OS X doesn’t come with the seq command, I would previously improvise.

# Linux system
$ for n in `seq 1 6`; do echo -n "web${n}: "; ssh web$n uptime; done;
web1: 18:36:56 up 82 days, 22:50,  4 users,  load average: 0.03, 0.00, 0.00
web2: 18:36:56 up 82 days, 22:48,  2 users,  load average: 0.07, 0.00, 0.00
web3: 18:36:56 up 82 days, 22:47,  3 users,  load average: 0.04, 0.00, 0.00
web4: 18:36:56 up 82 days, 22:47,  2 users,  load average: 0.03, 0.00, 0.00
web5: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00
web6: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00

# Mac
$ for n in `echo "1 2 3 4 5 6"`; do echo -n "web${n}"; ssh web$n uptime; done;
web1: 18:36:56 up 82 days, 22:50,  4 users,  load average: 0.03, 0.00, 0.00
web2: 18:36:56 up 82 days, 22:48,  2 users,  load average: 0.07, 0.00, 0.00
web3: 18:36:56 up 82 days, 22:47,  3 users,  load average: 0.04, 0.00, 0.00
web4: 18:36:56 up 82 days, 22:47,  2 users,  load average: 0.03, 0.00, 0.00
web5: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00
web6: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00

That is no longer the case. I could have written this myself, but since I am not for reinventing the wheel, I Google’d around and found count.

$ for n in `count 1 6`; do echo -n "web${n}"; ssh web$n uptime; done;
web1: 18:36:56 up 82 days, 22:50,  4 users,  load average: 0.03, 0.00, 0.00
web2: 18:36:56 up 82 days, 22:48,  2 users,  load average: 0.07, 0.00, 0.00
web3: 18:36:56 up 82 days, 22:47,  3 users,  load average: 0.04, 0.00, 0.00
web4: 18:36:56 up 82 days, 22:47,  2 users,  load average: 0.03, 0.00, 0.00
web5: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00
web6: 18:36:56 up 82 days, 22:45,  2 users,  load average: 0.01, 0.00, 0.00

I can’t take any credit for this script, nor do I plan to. But I feel that all Mac shell users should have this available to them. The original was written by Dave Taylor and is located here. I strongly suggest that in addition to copying the script, you read his explanation of its utility. It give you the ability to increment or decrement, whichever you find the need for.

To install, just copy and paste into /usr/local/bin/count and then chmod +x /usr/local/bin/count.

Here is the script in its entirety.

#!/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
Posted in Mac. Tags: , . 4 Comments »
  • Jon Tollerton

    What about doing this all within bash?

    for ((n=1;n<=6;n++)); do echo -n “web${n}: “; ssh web$n uptime; done;

  • http://eric.lubow.org Eric Lubow

    There is nothing wrong with that. But I have grown accustomed to as little typing as possible. Especially being used to having 'seq' in my toolkit from years of Bash usage in Linux. I wanted the equivalent shortcut on my Mac. I would use your way in a Bash script, but from a one-line from the command prompt, I would prefer having something like 'count' at my disposal.

  • Pingback: Count Instead of Sequence | BeginnerPC

  • Pingback: Tweets that mention Count Instead of Sequence | Erics Tech Blog -- Topsy.com