November 11, 2009, 12:51 PM — Since I've been using Solaris since the dawn of time (as far back as 1983 anyway when SunOS wasn't yet called "Solaris") and use Linux systems only now and then, I have only just learned about the seq command. The seq command doesn't do a whole lot. It just generates a sequence of numbers. Even so, it saves me a lot of annoying little issues in my scripts, such as whether I need to use "less than" (-lt) or "less than or equal to" (-le) and whether I have incremented my loop counter correctly and in the right place in my loop or initialized it in the first place.
On Linux systems, "seq 5 11" will generate a list of numbers starting with 5 and ending with 11.
seq 5 11 5 6 7 8 9 10 11
Now, doesn't that just make your day? OK, maybe not. But, think how nice it would be to turn a script like this:
# opens 4 terminal windows
i="0"
while [ $i -lt 4 ]
do
xterm &
i=$[$i+1]
done
into one like this:
# opens 4 terminal windows
while [ seq 1 4 ]
do
xterm &
done
Less annoyance is always a good thing. It leaves some of those diminishing brain cells free for harder tasks.
Of course after discovering seq on a Linux system (thanks to a student of mine -- Hi, Jeremy!), I went back to my beloved Solaris box and ... no seq!
Being unwilling to go back to incrementing loop counters (at least not more than one more time), I decided to implement seq in bash and drop my script in /usr/local/bin. Should be nearly as easy to do this in C and compile the little darling.
#!/bin/bash
if [ $# != 2 ]; then
echo "USAGE: $0 <begin> <end>"
exit 1
else
curr=$1
end=$2
fi
while [ $curr -le $end ]
do
echo $curr
curr=$[$curr+1]
done
Of course not all while loops loop on integers, never mind sequential integers, but this little "trick" is a nice one to use when they do.


















