2018-11-13 21:59:36 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
2018-11-14 13:20:56 +00:00
|
|
|
category-title: Scripting
|
2018-11-13 21:59:36 +00:00
|
|
|
tags: loop for done script scripting
|
|
|
|
author: Matteo Omenetti
|
|
|
|
title: For Loop
|
|
|
|
---
|
|
|
|
|
|
|
|
<!-- Introduction -->
|
|
|
|
The second type of loops are for loops.
|
2018-11-15 15:00:14 +00:00
|
|
|
They follow this syntax:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 21:59:36 +00:00
|
|
|
for [variable] in [list] do
|
|
|
|
[code]
|
|
|
|
done
|
2018-11-14 21:15:38 +00:00
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
Their purpose is to <i>iterate</i> over a list. Also, while loops could do this,
|
|
|
|
you might argue... <br>
|
2018-11-15 15:00:14 +00:00
|
|
|
Of course, they could, but for loops are specifically meant to do this. Therefore, for instance,
|
|
|
|
you don't have to declare your counter variable outside the loop. Most importantly,
|
2018-11-13 21:59:36 +00:00
|
|
|
this variable can be accessed from inside the loop. <br>
|
2018-11-14 21:15:38 +00:00
|
|
|
|
2018-11-13 21:59:36 +00:00
|
|
|
For loops take this form:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 21:59:36 +00:00
|
|
|
for VARIABLE in 1 2 3 4 5 .. N
|
|
|
|
do
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
commandN
|
|
|
|
done
|
2018-11-14 21:15:38 +00:00
|
|
|
{% endhighlight %}
|
2018-11-13 21:59:36 +00:00
|
|
|
<!-- End of Introduction -->
|
|
|
|
|
|
|
|
<!-- First Example -->
|
|
|
|
Here is a simple example:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 21:59:36 +00:00
|
|
|
for i in 1 2 3 4 5
|
|
|
|
do
|
|
|
|
echo "Welcome $i times"
|
|
|
|
done
|
2018-11-14 21:15:38 +00:00
|
|
|
{% endhighlight %}
|
2018-11-13 21:59:36 +00:00
|
|
|
|
|
|
|
This first example of code simply displays a welcome message 5 times.
|
|
|
|
The output of this piece of code is:
|
|
|
|
<pre>
|
2018-11-14 21:15:38 +00:00
|
|
|
Welcome 1 times
|
|
|
|
Welcome 2 times
|
|
|
|
Welcome 3 times
|
|
|
|
Welcome 4 times
|
|
|
|
Welcome 5 times
|
2018-11-13 21:59:36 +00:00
|
|
|
</pre>
|
|
|
|
<!-- End of First Example -->
|
|
|
|
|
2018-11-15 15:00:14 +00:00
|
|
|
<!-- Explanation of Numerical Range -->
|
2018-11-15 20:09:21 +00:00
|
|
|
There are also other ways to specify the <i> numerical range </i>. For instance, if
|
|
|
|
your numerical range is too big, you can simply write: <code> {1..100} </code>. This piece
|
|
|
|
of code means every natural number between 1 and 100 (both included). <br>
|
2018-11-13 21:59:36 +00:00
|
|
|
Ranges can also count backward like this: <code>{10..1}</code>.
|
2018-11-15 15:00:14 +00:00
|
|
|
You can even increment the numerical value by step of two: <code> {0..10..2} </code>.
|
2018-11-13 21:59:36 +00:00
|
|
|
This piece of code means every natural number between 0 and 10 with a step of two,
|
|
|
|
0 2 4 6... 10.
|
2018-11-15 15:00:14 +00:00
|
|
|
<!-- End of Explanation of Numerical Range -->
|