Linux and Networking

Linux and Networking

4.6. Repetitive sentences. Loops

In shell script there are three very useful repetitive statements: for, while and until statements. Let’s look carefully at each of these sentences:

 

  • for – The for statement is used when you want to execute a series of statements a certain number of times. The for statement in shell script is a little different from the for statement in other programming languages. An advantage of the for shell script is that you can iterate over strings (we will see how this is done later).
  • while – This statement executes a piece of code if a condition is met, and stops when the condition is false
  • until – Very similar to the previous one. The until statement executes a piece of code until an expression is false. The difference with the previous one is that while while asks and then executes, until first executes and then asks. Note that any until statement can be converted to a while statement.

 

Let’s look at an example of the for statement:

In this case, all the files in the directory from where the previous script is executed are shown. The variable i takes as its value one by one the name of the files in the current directory. As you can see, within the for (from do to done) the content of the variable i is shown.

 

Suggested exercise.

Copy and execute the previous exercise. Check that it works and modify it to show another directory of your choice.

 

Let’s look at another example. The above example is not very similar to the for loops of other languages like java, C, etc. The following example does resemble the for of these languages:

The command seq 1 10 generates a sequence that goes from 1 to 10. If you look, what this script will do is display the numbers from 1 to 10 on the screen, both included.

 

Suggested exercise.

Copy and execute the previous exercise. Check that it works and modify it to display the numbers 5 to 50.

 

Would you be able to make it so that only even numbers are shown?

 

Well, now that we know how the for loop works, let’s move on to see how the while and until loops work. If you have understood the for it will not cost you anything to learn these others. Let’s first see how the while loop works with an example:

In this example we have a variable called COUNTER that initially has a value of 0 and then increases one by one until it reaches 10. The result of executing the previous script is the following:

 

The counter is 0

The counter is 1

The counter is 2

The counter is 3

The counter is 4

The counter is 5

The counter is 6

The counter is 7

The counter is 8

The counter is 9

 

It does not display the value 10 because the condition is COUNTER -lt 10 (lt is less than). We will see later how the conditions work.

 

Let’s now see an example with until:

The result of running the above script is this:

 

COUNTER: 20

COUNTER: 19

COUNTER: 18

COUNTER: 17

COUNTER: 16

COUNTER: 15

COUNTER: 14

COUNTER: 13

COUNTER: 12

COUNTER: 11

COUNTER: 10

 

Suggested exercises.

  1. Make a script that displays even numbers from 0 to 100 with while.
  2. Perform the same exercise in a descending manner but using until.
The Demeter Project