60555b37a6
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@165 a672b425-5310-4d7a-af5c-997e18724b81
124 lines
3.5 KiB
HTML
124 lines
3.5 KiB
HTML
---
|
|
layout: page
|
|
category-page: scripts
|
|
category:title Scripting
|
|
tags: loop while do script scripting read
|
|
author: Matteo Omenetti
|
|
title: While Loop
|
|
---
|
|
<!-- Introduction -->
|
|
Loops are an important concept in programming and therefore also in scripting. Thanks to loops you are able to repeat an instruction
|
|
automatically several times, until a certain condition turns false. <br>
|
|
|
|
Two are the main types of loops: while and for. They both generate a repeating piece of code, but with some key differences
|
|
that make them suitable for some different needs while programming. <br>
|
|
|
|
While loops take this form:
|
|
<pre>
|
|
while [condition]
|
|
do
|
|
command1
|
|
command2
|
|
command3
|
|
...
|
|
done
|
|
</pre>
|
|
<!-- End of Introduction -->
|
|
|
|
<!-- First Example -->
|
|
Here is a first simple example:
|
|
<pre>
|
|
i=0;
|
|
|
|
while [$i -lt 4]
|
|
do
|
|
echo $i
|
|
i=$((i + 1))
|
|
done
|
|
</pre>
|
|
|
|
In this first example, you simply create a variable called i and evaluate it to 0.
|
|
Then you acces the while loop: the condition <code> [$i -lt 4] </code> means while the varibale
|
|
i is less than 4. every cycle of this loop, you print out the value of variable i with <code> echo $i </code>
|
|
and finally you increase its value by 1 with <code> i=$((i + 1)) </code>.
|
|
Therefore in 4 cycles the value of i will be 4. This will make the condition of the while loop false.
|
|
The output of this piece of code is:
|
|
<pre>
|
|
0
|
|
1
|
|
2
|
|
3
|
|
</pre>
|
|
<!-- End of First Example -->
|
|
|
|
<!-- Second Example -->
|
|
Sometimes it required to decleare infinite loops for various programming purposes. <br>
|
|
Here is an example:
|
|
|
|
<pre>
|
|
i=1
|
|
|
|
while :
|
|
do
|
|
printf "i=$i\i: Hello World"
|
|
if [ $i == 3 ]
|
|
then
|
|
echo "I love programming"
|
|
elif [ $i == 5]
|
|
then
|
|
echo "I love Bash"
|
|
elif [ $i == 7 ]
|
|
then
|
|
echo "I love this website"
|
|
elif [ $i == 9 ]
|
|
then
|
|
exit 0
|
|
i=$((i + 1))
|
|
done
|
|
</pre>
|
|
No termination codinition is set for this loop in this example. This type of loop is called infinite loop.
|
|
The <code> exit </code> statement is used to quit the loop. This loop will iterate for 9 times, then
|
|
as soon as i becomes equal to 0, the condtition of the last if statement will evaluate to true and the
|
|
loop will be terminated. <br>
|
|
The output of this piece of code is:
|
|
<pre>
|
|
1: Hello World
|
|
2: Hello World
|
|
3: Hello World
|
|
I Love programming
|
|
4: Hello World
|
|
5: Hello World
|
|
I love Bash
|
|
6: Hello World
|
|
7: Hello World
|
|
I love this website
|
|
8: Hello World
|
|
9: Hello World
|
|
</pre>
|
|
<!-- End of Second Example -->
|
|
|
|
If you want your shell to hang forever doing nothing you can write out the following infinite loop:
|
|
|
|
<pre>
|
|
while :
|
|
do
|
|
:
|
|
done
|
|
</pre>
|
|
|
|
|
|
In scripting, while loops, are often used to process files line by line. <br>
|
|
Here is an examaple:
|
|
<pre>
|
|
while read -r first_name last_name phone;
|
|
do
|
|
printf '%s\n' "$last_name"
|
|
done < "$file"
|
|
</pre>
|
|
|
|
The <code> read </code> command is used to read file line by line. The flag <code> -r </code> is used to tell the
|
|
command read to intepret backslashes (/) literally, instead as escape characters. This command, expect for some few
|
|
rare cases, should always be used with this flag.
|
|
In this example, <code> < "$file" </code> redirects the loop's input from a file whose name is stored in a variable.
|
|
This file has 3 colums, <code> first_name last_name phone </code>, separated by blank space (or a tab).
|
|
This piece of code only prints out the second column.
|