Loops!

Round and round we go.

Introduction

Bash loops are very useful. In this section of our Bash Scripting Tutorial we'll look at the different loop formats available to us as well as discuss when and why you may want to use each of them.

Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks.

There are 3 basic loop structures in Bash scripting which we'll look at below. There are also a few statements which we can use to control the loops operation.

While Loops

One of the easiest loops to work with is while loops. They say, while an expression is true, keep executing these lines of code. They have the following format:

while [ <some test> ]
do
<commands>
done

You'll notice that similar to if statements the test is placed between square brackets [ ].

While loop diagram

In the example below we will print the numbers 1 through to 10:

while_loop.sh

  1. #!/bin/bash
  2. # Basic while loop
  3. counter=1
  4. while [ $counter -le 10 ]
  5. do
  6. echo $counter
  7. ((counter++))
  8. done
  9. echo All done

Let's break it down:

  • Line 4 - We'll initialise the variable counter with it's starting value.
  • Line 5 - While the test is true (counter is less than or equal to 10) let's do the following commands.
  • Line 7 - We can place any commands here we like. Here echo is being used as it's an easy way to illustrate what is going on.
  • Line 8 - Using the double brackets we can increase the value of counter by 1.
  • Line 9 - We're at the bottom of the loop so go back to line 5 and perform the test again. If the test is true then execute the commands. If the test is false then continue executing any commands following done.
  1. ./while_loop.sh
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. All done

A common mistake is what's called an off by one error. In the example above we could have put -lt as opposed to -le (less than as opposed to less than or equal). Had we done this it would have printed up until 9. These mistakes are easy to make but also easy to fix once you've identified it so don't worry too much if you make this error.

Until Loops

The until loop is fairly similar to the while loop. The difference is that it will execute the commands within it until the test becomes true.

until [ <some test> ]
do
<commands>
done

until_loop.sh

  1. #!/bin/bash
  2. # Basic until loop
  3. counter=1
  4. until [ $counter -gt 10 ]
  5. do
  6. echo $counter
  7. ((counter++))
  8. done
  9. echo All done

As you can see in the example above, the syntax is almost exactly the same as the while loop (just replace while with until). We can also create a script that does exactly the same as the while example above just by changing the test accordingly.

So you may be asking, 'Why bother having the two different kinds of loops?'. We don't necessarily. The while loop would be able to handle every scenario. Sometimes, however, it just makes it a little easier to read if we phrase it with until rather than while. Think about the following statement:

Leave the towel on the line until it's dry.

We could have said:

Leave the towel on the line while it is not dry.

Or:

Leave the towel on the line while it is wet.

But they just don't seem as elegant and easy to understand. So by having both while and until we can pick whichever one makes the most sense to us and as a result, end up with code that is easier for us to understand when we read it.

We should always strive for clean, obvious and elegant code when writing our Bash scripts.

For Loops

The for loop is a little bit different to the previous two loops. What it does is say for each of the items in a given list, perform the given set of commands. It has the following syntax.

for var in <list>
do
<commands>
done

The for loop will take each item in the list (in order, one after the other), assign that item as the value of the variable var, execute the commands between do and done then go back to the top, grab the next item in the list and repeat over.

The list is defined as a series of strings, separated by spaces.

Here is a simple example to illustrate:

for_loop.sh

  1. #!/bin/bash
  2. # Basic for loop
  3. names='Stan Kyle Cartman'
  4. for name in $names
  5. do
  6. echo $name
  7. done
  8. echo All done

Let's break it down:

  • Line 4 - Create a simple list which is a series of names.
  • Line 6 - For each of the items in the list $names assign the item to the variable $name and do the following commands.
  • Line 8 - echo the name to the screen just to show that the mechanism works. We can have as many commands here as we like.
  • Line 11 - echo another command to show that the bash script continued execution as normal after all the items in the list were processed.
  1. ./for_loop.sh
  2. Stan
  3. Kyle
  4. Cartman
  5. All done

Ranges

We can also process a series of numbers

for_loop_series.sh

  1. #!/bin/bash
  2. # Basic range in for loop
  3. for value in {1..5}
  4. do
  5. echo $value
  6. done
  7. echo All done
  • Line 4 - It's important when specifying a range like this that there are no spaces present between the curly brackets { }. If there are then it will not be seen as a range but as a list of items.
  1. ./for_loop_series.sh
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. All done

When specifying a range you may specify any number you like for both the starting value and ending value. The first value may also be larger than the second in which case it will count down.

It is also possible to specify a value to increase or decrease by each time. You do this by adding another two dots ( .. ) and the value to step by.

for_loop_stepping.sh

  1. #!/bin/bash
  2. # Basic range with steps for loop
  3. for value in {10..0..2}
  4. do
  5. echo $value
  6. done
  7. echo All done
  1. ./for_loop.sh
  2. 10
  3. 8
  4. 6
  5. 4
  6. 2
  7. 0
  8. All done

On some versions of Bash the above may not work. If that is the case for you then you can also try the following alternative syntax :

for_loop_alternate_syntax.sh

  1. #!/bin/bash
  2. # Basic range with alternate syntax
  3. for ((num = 1; num <= 5; num++))
  4. do
  5. echo $num
  6. done
  7. echo All done
  1. ./for_loop_alternate_syntax.sh
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. All done

One of the more useful applications of for loops is in the processing of a set of files. To do this we may use wildcards. Let's say we want to convert a series of .html files over to .php files.

convert_html_to_php.sh

  1. #!/bin/bash
  2. # Make a php copy of any html files
  3. for value in $1/*.html
  4. do
  5. cp $value $1/$( basename -s .html $value ).php
  6. done

Controlling Loops: Break and Continue

Most of the time your loops are going to through in a smooth and ordely manner. Sometimes however we may need to intervene and alter their running slightly. There are two statements we may issue to do this.

Break

The break statement tells Bash to leave the loop straight away. It may be that there is a normal situation that should cause the loop to end but there are also exceptional situations in which it should end as well. For instance, maybe we are copying files but if the free disk space get's below a certain level we should stop copying.

copy_files.sh

  1. #!/bin/bash
  2. # Make a backup set of files
  3. for value in $1/*
  4. do
  5. used=$( df $1 | tail -1 | awk '{ print $5 }' | sed 's/%//' )
  6. if [ $used -gt 90 ]
  7. then
  8. echo Low disk space 1>&2
  9. break
  10. fi
  11. cp $value $1/backup/
  12. done

Continue

The continue statement tells Bash to stop running through this iteration of the loop and begin the next iteration. Sometimes there are circumstances that stop us from going any further. For instance, maybe we are using the loop to process a series of files but if we happen upon a file which we don't have the read permission for we should not try to process it.

copy_check.sh

  1. #!/bin/bash
  2. # Make a backup set of files
  3. for value in $1/*
  4. do
  5. if [ ! -r $value ]
  6. then
  7. echo $value not readable 1>&2
  8. continue
  9. fi
  10. cp $value $1/backup/
  11. done

Select

The select mechanism allows you to create a simple menu system. It has the following format:

select var in <list>
do
<commands>
done

When invoked it will take all the items in list (similar to other loops this is a space separated set of items) and present them on the screen with a number before each item. A prompt will be printed after this allowing the user to select a number. When they select a number and hit enter the corresponding item will be assigned to the variable var and the commands between do and done are run. Once finished a prompt will be displayed again so the user may select another option.

A few points to note:

  • No error checking is done. If the user enters something other than a number or a number not corresponding to an item then var becomes null (empty)
  • If the user hits enter without entering any data then the list of options will be displayed again.
  • The loop will end when an EOF signal is entered or the break statement is issued.
  • You may change the system variable PS3 to change the prompt that is displayed.

Here is a simple example to illustrate it's usage:

select_example.sh

  1. #!/bin/bash
  2. # A simple menu system
  3. names='Kyle Cartman Stan Quit'
  4. PS3='Select character: '
  5. select name in $names
  6. do
  7. if [ $name == 'Quit' ]
  8. then
  9. break
  10. fi
  11. echo Hello $name
  12. done
  13. echo Bye

Let's break it down:

  • Line 4 - Set up a variable with the list of characters and a last option which we may select to quit. Note that the items are separated by a space.
  • Line 6 - Change the value of the system variable PS3 so that the prompt is set to something a little more descriptive. (By default it is #?)
  • Lines 10 - 13 - If the last option, 'Quit', is selected then break out of the select loop.
  • Line 14 - Print out a message just to demonstrate the mechanism has worked. You may have as many commands here as you like.
  • Line 17 - Print a message just to show that the script has continued as normal after the select loop.

And now let's run the Bash script:

  1. ./select_example.sh
  2. 1) Kyle     3) Stan
  3. 2) Cartman  4) Quit
  4. Select character: 2
  5. Hello Cartman
  6. Select Character: 1
  7. Hello Kyle
  8. Select character: 4
  9. Bye

Summary

while do done
Perform a set of commands while a test is true.
until do done
Perform a set of commands until a test is true.
for do done
Perform a set of commands for each item in a list.
break
Exit the currently running loop.
continue
Stop this iteration of the loop and begin the next iteration.
select do done
Display a simple menu system for selecting items from a list.
Clarity
There are several Bash loop mechanisms. Pick the one which makes your code the easiest to follow.
Planning
Now that your scripts are getting a little more complex you will probably want to spend a little bit of time thinking about how you structure them before diving in.

Activities

Now we have quite a collection of tools under our belt, we can tackle some more interesting problems.

  • Create a simple script which will print the numbers 1 - 10 (each on a separate line) and whether they are even or odd.
  • Write a Bash script which will take a single command line argument (a directory) and will print each entry in that directory. If the entry is a file it will print it's size. If the entry is a directory it will print how many items are in that directory.
  • Create a command line version of the game Mastermind. Instead of coloured marbles you could use letters or numbers or be creative and find another way.
  • Create a command line version of the game Tic Tac Toe. Make it so you can play against the computer.