2018-11-13 21:00:00 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
2018-11-14 13:20:56 +00:00
|
|
|
category-title: Scripting
|
2018-11-13 21:00:00 +00:00
|
|
|
tags: loop while do script scripting read
|
|
|
|
author: Matteo Omenetti
|
|
|
|
title: While Loop
|
|
|
|
---
|
2018-11-15 13:25:55 +00:00
|
|
|
|
2018-11-13 21:00:00 +00:00
|
|
|
<!-- Introduction -->
|
2018-11-14 21:15:38 +00:00
|
|
|
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>
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
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 different needs while programming.<br>
|
2018-11-13 21:00:00 +00:00
|
|
|
|
|
|
|
While loops take this form:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
while [condition]
|
|
|
|
do
|
2018-11-13 21:00:00 +00:00
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
2018-11-14 21:15:38 +00:00
|
|
|
done
|
|
|
|
{% endhighlight %}
|
2018-11-13 21:00:00 +00:00
|
|
|
<!-- End of Introduction -->
|
|
|
|
|
|
|
|
<!-- First Example -->
|
2018-11-14 21:15:38 +00:00
|
|
|
Here is a first simple example:
|
|
|
|
{% highlight bash %}
|
|
|
|
i=0;
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
while [$i -lt 4]
|
|
|
|
do
|
|
|
|
echo $i
|
|
|
|
i=$((i + 1))
|
|
|
|
done
|
|
|
|
{% endhighlight %}
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
In this first example, you simply create a variable called i and evaluate it to 0.
|
2018-11-15 15:00:14 +00:00
|
|
|
Then you access the while loop: the condition <code> [$i -lt 4] </code> means that this while
|
|
|
|
loop will run until the <code> i </code> variable is less than 4.
|
2018-11-14 21:15:38 +00:00
|
|
|
Every cycle of this loop, you print out the value of variable i with <code> echo $i </code>
|
2018-11-15 15:00:14 +00:00
|
|
|
and finally, you increase its value by 1 with <code> i=$((i + 1)) </code>.
|
2018-11-14 21:15:38 +00:00
|
|
|
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>
|
2018-11-13 21:00:00 +00:00
|
|
|
0
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
2018-11-14 21:15:38 +00:00
|
|
|
</pre>
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
<!-- End of First Example -->
|
2018-11-13 21:00:00 +00:00
|
|
|
|
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
<!-- Infinite Loop First Example -->
|
2018-11-15 15:00:14 +00:00
|
|
|
Sometimes it is required to declare infinite loops for various programming purposes. <br>
|
2018-11-14 21:15:38 +00:00
|
|
|
Here is an example:
|
|
|
|
{% highlight bash %}
|
|
|
|
i=1;
|
|
|
|
|
|
|
|
while :
|
|
|
|
do
|
2018-11-13 21:00:00 +00:00
|
|
|
printf "i=$i\i: Hello World"
|
2018-11-14 21:15:38 +00:00
|
|
|
if [ $i == 3 ]; then
|
|
|
|
echo "I love DrRacket"
|
|
|
|
elif [ $i == 5]; then
|
|
|
|
echo "I love Bash"
|
|
|
|
elif [ $i == 7 ]; then
|
|
|
|
echo "I love this website"
|
|
|
|
elif [ $i == 9 ]; then
|
|
|
|
exit 0
|
2018-11-13 21:00:00 +00:00
|
|
|
i=$((i + 1))
|
2018-11-14 21:15:38 +00:00
|
|
|
done
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
No termination condition is set for this loop in this example. This type of loop
|
|
|
|
is called infinite loop.<br>
|
|
|
|
The <code> exit </code> statement is used to quit the loop. This loop will
|
|
|
|
iterate for 9 times, then
|
2018-11-15 15:00:14 +00:00
|
|
|
as soon as <code> i </code> becomes equal to 0, the condition of the last if
|
2018-11-14 21:15:38 +00:00
|
|
|
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>
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-13 21:59:36 +00:00
|
|
|
<!-- End of Infinite Loop First Example -->
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-13 21:59:36 +00:00
|
|
|
<!-- Infinite Loop Second Example -->
|
2018-11-14 21:15:38 +00:00
|
|
|
If you want your shell to hang forever doing nothing you can write out the following infinite loop:
|
|
|
|
{% highlight bash %}
|
|
|
|
while :
|
|
|
|
do
|
2018-11-13 21:00:00 +00:00
|
|
|
:
|
2018-11-14 21:15:38 +00:00
|
|
|
done
|
|
|
|
{% endhighlight %}
|
|
|
|
<!-- End of Infinite Loop Second Example -->
|
2018-11-13 21:00:00 +00:00
|
|
|
|
2018-11-13 21:59:36 +00:00
|
|
|
<!-- Read -->
|
2018-11-14 21:15:38 +00:00
|
|
|
In scripting, while loops are often used to process files line by line. <br>
|
2018-11-15 15:00:14 +00:00
|
|
|
Here is an example:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
while read -r first_name last_name phone;
|
|
|
|
do
|
|
|
|
printf '%s\n' "$last_name"
|
|
|
|
done < "$file"
|
|
|
|
{% endhighlight %}
|
2018-11-13 21:59:36 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
The <code> read </code> command is used to read a file line by line.
|
|
|
|
The flag <code> -r </code> is used to tell the
|
2018-11-15 15:00:14 +00:00
|
|
|
command read to interpret backslashes (/) literally, instead as escape characters.
|
2018-11-14 21:15:38 +00:00
|
|
|
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.
|
2018-11-15 15:00:14 +00:00
|
|
|
This file has 3 columns, <code> first_name last_name phone </code>, separated by
|
2018-11-14 21:15:38 +00:00
|
|
|
blank space (or a tab).
|
|
|
|
This piece of code only prints out the second column.
|
|
|
|
<!-- End of Read -->
|