Posted on 03/05/2019 7:25:18 AM PST by ShadowAce
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.
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 [ ].
In the example below we will print the numbers 1 through to 10:
Let's break it down:
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.
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
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.
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:
Let's break it down:
We can also process a series of numbers
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.
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.
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.
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.
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.
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:
Here is a simple example to illustrate it's usage:
Let's break it down:
And now let's run the Bash script:
Now we have quite a collection of tools under our belt, we can tackle some more interesting problems.
Loops are bad
Loops are a pain
Loops make you do things
again and again
... sorry wrong type of bash
An imminently practical function. One habitual task I use it for is to mute system sound on my PC for a prescribed period of time. Very handy when listening to streaming talk radio because they all seem to only be able to sell ads for men’s virility pills and ambulance-chasing lawyers, which I choose not to listen to.
At invocation, the script mutes the master system sound volume, then asks me how long I want to mute it for. Then it uses a while/if loop of the “sleep” function to count down that many seconds before un-muting.
I wrote similar batch file for Windows using if/then looping. It’s such a relief not to have to listen to that moronic woman teach me how to spell the name of her dog food.
In the following the script checks for the file /tmp/i_exist. If it is there, it sleeps a bit and checks again. Once the file is deleted, it exits. You can use this as a poor man's job control. Let's say you have a job that kicks off every day and does some work. Let's also say that you have a separate job that needs to run, but it is dependent upon the first job finishing successfully before it can run. You can have the first job create a check file, and delete it when it finishes, which will allow the 2nd to execute.
$ cat loopers #!/bin/bash while [ -f /tmp/i_exist ] do echo "It exists" sleep 5 done echo "It no longer exists. Exiting" exit 0 $ ./loopers It exists It exists It exists It exists It no longer exists. Exiting $
Another use for something like this is to test to see if a job is already running. You could have the script create a run file. If the job is running it exists. If you start the same job a second time, it could see that the file already exists, and refuse to run. There are other possible ways to do this as well, but this is quick and dirty, and generally works, as long as the job completes successfully and cleans up after itself. If the script is killed while executing, it might not do so, so subsequent executions will see the file and thus refuse to run.
$ cat loopers #!/bin/bash if [ -f /tmp/i_exist ] do echo "Check file exists! Check to see if another copy of this program is running." exit 1 done echo "Now I can create the file" touch /tmp/i_exist echo "Real work would be done here, since the file didn't exist." echo "Now I clean up after myself..." rm /tmp/i_exist exit 0
Note that above I used "if" instead of 'while'. Either would work in this instance.
Syntax error. In the second script the “do” should be “then”. That’s what I get for writing on the fly without testing.
Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.