UNIX Tutorials, Tips, Tricks and Shell Scripts

5 UNIX for and while Loop Examples...with Sample Shell Scripts!!!


UNIX shell script for and while loops are key weapons in every shell programmer's arsenal. Although there are two additional looping constructs, select and until, I have almost always been able to efficiently accomplish my UNIX shell scripting objectives with either a for loop or a while loop.

Come to think of it, I do not recall ever having the need to use a select or until loop in any shell scripts that I've written in the past. It's very possible that the positions I've worked in, and therefore the shell scripting related tasks I've needed to complete, didn't require the use of select or until loops but I'm sure that both constructs serve an important role...somewhere.

In preparation for writing this piece, I've thought about how I've used for and while loops over the past 15 years and more importantly, since memories...even those of cherished shell scripts...tend to fade away, I spent time reviewing the most recent group of scripts I wrote.

Let's get started...

The UNIX Shell Script for Loop

Although there are two variants of the for loop...

for variable [ in list_of_items ]
do
   statement 1
   statement 2
   statement 3
   ...
   statement n
done
and

for (( variable_initialization; loop_condition; variable_update ))
do
   statement 1
   statement 2
   statement 3
   ...
   statement n
done
...I have seldomly used the second one. This variant should look very familiar to you if you have written any code with the C programming language.

As the syntax for the first format indicates, including the keyword in is optional. I have always included the in keyword followed by a list_of_items, but there are likely valid use cases for creating a for loop without it.

Enough of the formalities, let's jump right into a use case with sample shell scripts to demonstrate usage!


Example 1a - How to write a UNIX shell script with a for loop to read and process each value in a text file

More often then not, a for loop has been most useful to me when there was a need to perform a task against a large number of host computers. This was true regardless if the task was related to system administration work or the development of complex test automation code.

The names of the host computers (hostnames) would need to be listed in the for loop's list_of_items. Right now you are probably thinking to yourself, "Do I really need to list out hundreds, possibly thousands, of hostnames in a for loop...and what if there are multiple for loops in a single shell script?" The answer is no, it will not be necessary.



You can simply create a text file, containing a single hostname on each line of the file, that your shell script's for loop will cycle through. Here's a sample shell script (with text file) to demonstrate how this is done:

$ cat myhosts
happy
sleepy
sneezy
grumpy
dopey
bashful
doc

$ cat for_loop_ex1a
#!/bin/ksh

HOSTFILE=myhosts

for host in $(cat $HOSTFILE)
do
   print $host
done
Now let's run the shell script...

$ ./for_loop_ex1a
happy
sleepy
sneezy
grumpy
dopey
bashful
doc
This simple example loops through each value (host) in myhosts, which is assigned to the shell variable HOSTFILE, and prints the hostname to standard output. Your scripts will obviously do more than just print the value stored in the for loop's variable, but I think you get the idea.

In a minute we will come back to make this script more interesting, but you will need to have a basic understanding of the while loop first.


The UNIX Shell Script while Loop

A while loop will repeat until the the while statement evaluates to false OR a break statement within the body of the loop is executed. A continue statement within the body of the while loop will cause the current iteration of the loop to terminate and the loop's while statement will be evaluated to see if another iteration of the loop should be run.

The syntax for the while loop is as follows...

while
do
   statement 1
   statement 2
   statement 3
   ...
   statement n
done

Example 2 - How to write a UNIX shell script with a simple while loop that repeats for 5 iterations

This simple script will loop while the value for the variable loopcount is less than or equal to ("-le") the value of the loopmax variable.

$ cat while_loop_ex2
#!/bin/ksh

loopcount=1
loopmax=5
while [ $loopcount -le $loopmax ]
do
   print "loop iteration: $loopcount"
   (( loopcount=loopcount+1 ))
done
Running this script produces...

$ ./while_loop_ex2
loop iteration: 1
loop iteration: 2
loop iteration: 3
loop iteration: 4
loop iteration: 5
Note that less than or equal to ("-le") was used instead of less than ("-lt"). If the less than operator was used, the loop would only repeat 4 times.

The second while loop example will likely be more useful to you.



Example 3 - How to write a UNIX shell script with a while loop that reads each line in a text file

Shell scripts will frequently need to read the contents of a file, line by line, and store each line in a shell variable for additional processing. Using a while loop coupled with a read statement is a perfect way to accomplish this task.

The sample script for this example will utilize the same text file ("myhosts") that was used for the first for loop example (1a) above.

$ cat while_loop_ex3
#!/bin/ksh

HOSTFILE=myhosts

while read line
do
   TMPLINE=$line
   print $TMPLINE
done < $HOSTFILE
And the output for this script...

$ ./while_loop_ex3
happy
sleepy
sneezy
grumpy
dopey
bashful
doc
Although this script seems to accomplish the same outcome as our first for loop example, it does a little more. Consider what would happen if we added a space followed by the name of the operating system running on the host to the end of each line (after the hostname) in the input text file so that it looked like this:

$ cat myhosts
happy solaris
sleepy linux
sneezy hpux
grumpy aix
dopey aix
bashful linux
doc solaris

A run of the script from for loop example 1a would produce the following...

$ ./for_loop_ex1a
happy
solaris
sleepy
linux
sneezy
hpux
grumpy
aix
dopey
aix
bashful
linux
doc
solaris
whereas running our while loop script, which reads the lines from the file, results in more desirable output...

$ ./while_loop_ex3
happy solaris
sleepy linux
sneezy hpux
grumpy aix
dopey aix
bashful linux
doc solaris
The while loop gives you the ability to work and manipulate all of the information tied to a single host (line of text), which is read into the "line" shell variable.

Now that you have a basic understanding of while loop syntax and behavior, let's return to the for loop for a second example related to that construct.


Example 1b - How to write a UNIX shell script with a for loop that stores each value from a text file into a shell script array

To spice up the first for loop sample script, it will be modified to store each hostname in an array so that it can be easily accessed and associated with other arrays relevant to that particular hostname later on in the script.

Here is the updated shell script...

$ cat for_loop_ex1b
#!/bin/ksh

HOSTFILE=myhosts

i=0
for host in $(cat $HOSTFILE)
do
   HOST[${i}]=$host
   (( i=i+1 ))
done

hostcount=${#HOST[*]}
print "\n$hostcount hostnames have been stored in the array."
print "\nThe hostnames stored in the array are:\n"

j=0
while [ $j -lt $hostcount ]
do
   print ${HOST[$j]}
   (( j=j+1 ))
done

which produces...

$ ./for_loop_ex1b

7 hostnames have been stored in the array.

The hostnames stored in the array are:

happy
sleepy
sneezy
grumpy
dopey
bashful
doc

Bonus Tip Related to UNIX Shell Script for and while loops

You likely already know this, but remember that you do not need a shell script to benefit from the for and while loop constructs. Yes, you can run them at the command line prompt!

Here's an example derived from the first for loop example above...

$ for host in $(cat myhosts)
> do
> print $host
> done
happy
sleepy
sneezy
grumpy
dopey
bashful
doc
$
Note that the actual file name ("myhosts"), instead of a shell variable name, was used in the for loop's "cat" statement.

Also, the "Enter" key on my keyboard was pressed after each line of "code" was typed in. The ">" (greater than sign) in the example was created by the shell, NOT me. This is called the "secondary prompt," which is stored in the PS2 shell variable.

Do you need to learn UNIX shell scripting and get practice writing & running scripts...on a REAL SERVER? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to a real server in our Internet Lab for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.

Thanks for reading, and keep it loopy!!!