Looping the Easy Way

The seq command doesn't do a lot, but it saves a lot of annoying little issues in scripts

By Sandra Henry-Stocker  4 comments

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.

4 comments

    Anonymous 45 weeks ago
    The first example looked wrong to me, and is:$ while [ seq 1 4 ]; do echo x; done-bash: [: 1: binary operator expectedBut this works:seq 1 4
    Anonymous 2 years ago
    Cygwin seq ( for one - it's just what I've got handy - ) lets the user specify an increment, and handles floating-point numbers too:> seq 1 2 1013579> seq 1 0.2 21.01.21.41.61.82.0
    Anonymous 2 years ago
    Sometimes we need to iterate to process files with some name followed by anumber with constant width and filled to the left with zeros, this can be doneeasily with seq :for n in `seq -f '%04.0f' 15 23`; do echo "filename = blah${n}"donefilename = blah0015filename = blah0016filename = blah0017filename = blah0018filename = blah0019filename = blah0020filename = blah0021filename = blah0022filename = blah0023
    Anonymous 2 years ago
    'seq' is pretty cool and allows incrementing, decrementing, etc. But for a simple loop, as described in your article, there is a simpler way.Use the bash builtin for a sequence expression '{x..y}'.Examples:$ for i in {1..5}; do echo $i; done12345$ for i in {a..e}; do echo $i; doneabcde

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

      As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

      White Paper

      ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

      Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

      White Paper

      Business Value of Blade

      The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

      White Paper

      Accelerate time to application value

      For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

      White Paper

      Converged Infrastructure for Dummies

      As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question