2018-11-14 13:36:20 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
|
|
|
category-title: Scripting
|
|
|
|
tags: if else script scripting read
|
|
|
|
author: Matteo Omenetti
|
2018-11-18 20:38:56 +00:00
|
|
|
title: If statement
|
|
|
|
previous-page: pages/scripts/6-while-loop.html
|
|
|
|
next-page: pages/scripts/8-redirection.html
|
2018-11-14 13:36:20 +00:00
|
|
|
---
|
2018-11-15 13:25:55 +00:00
|
|
|
|
2018-11-14 13:36:20 +00:00
|
|
|
<!-- Introduction -->
|
2018-11-15 20:09:21 +00:00
|
|
|
If statements allow us to make decisions in our Bash scripts. They allow us to
|
2018-11-19 15:05:42 +00:00
|
|
|
decide whether or not to run a piece of code based on a condition that we set. <br>
|
2018-11-14 21:15:38 +00:00
|
|
|
|
|
|
|
If statements take this form:
|
|
|
|
{% highlight bash %}
|
|
|
|
if [condition]; then
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
2018-11-14 13:36:20 +00:00
|
|
|
|
2018-11-15 15:00:14 +00:00
|
|
|
Anything between <code>then</code> and <code>fi</code> will be executed only if the condition
|
2018-11-14 13:36:20 +00:00
|
|
|
evaluates to true. <br>
|
|
|
|
Here is a simple example:
|
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
i=210;
|
2018-11-14 13:36:20 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
if [$i -ge 200]; then
|
|
|
|
echo "You chose a big number."
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
2018-11-14 13:36:20 +00:00
|
|
|
|
2018-11-15 15:00:14 +00:00
|
|
|
In this first example we evaluate a variable <code>i</code> to 105.
|
2018-11-14 21:15:38 +00:00
|
|
|
The <i> if statement </i> will print "You chose a big number"
|
2018-11-15 15:00:14 +00:00
|
|
|
only if the number contained in our variable <code>i</code> is <b>G</b>reater or
|
2018-11-14 21:15:38 +00:00
|
|
|
<b>E</b>qual to 200. <br>
|
2018-11-19 15:05:42 +00:00
|
|
|
This is the case, therefore the output of this piece of code will be:
|
2018-11-14 13:36:20 +00:00
|
|
|
<pre>
|
|
|
|
You chose a big number.
|
|
|
|
</pre>
|
|
|
|
<!-- End of Introduction -->
|
|
|
|
|
|
|
|
|
|
|
|
<!-- If Else -->
|
|
|
|
<h3> If Else </h3>
|
|
|
|
|
2018-11-15 15:00:14 +00:00
|
|
|
Sometimes we want to perform a certain set of actions, if our condition evaluates to
|
2018-11-14 21:15:38 +00:00
|
|
|
true and another set of actions if our condition evaluates to false. We can do this with
|
|
|
|
the <i> if else </i> statement.
|
2018-11-19 15:05:42 +00:00
|
|
|
<i> if else </i> statements take this form:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
if [condition]; then
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
else
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
2018-11-14 13:36:20 +00:00
|
|
|
|
|
|
|
Here is a simple example:
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
i=50;
|
|
|
|
|
|
|
|
if [$i -ge 200]; then
|
|
|
|
echo "You chose a big number."
|
|
|
|
else
|
|
|
|
echo "You chose a small number."
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
In this example, which is just an extension of the previous example, we
|
2018-11-15 15:00:14 +00:00
|
|
|
evaluate a variable <code>i</code> to 50. If <code>i</code> is greater or equal to
|
2018-11-19 15:05:42 +00:00
|
|
|
200, we print out "You chose a big number", otherwise,
|
|
|
|
(if <code>i</code> is not greater or equal to 200), just like in this case, we print out
|
2018-11-14 21:15:38 +00:00
|
|
|
"You chose a small number".
|
2018-11-15 15:00:14 +00:00
|
|
|
Therefore, the output of this piece of code is:
|
2018-11-14 21:15:38 +00:00
|
|
|
|
2018-11-14 13:36:20 +00:00
|
|
|
<pre>
|
|
|
|
You chose a small number.
|
|
|
|
</pre>
|
|
|
|
<!-- End of If Else -->
|
|
|
|
|
|
|
|
|
|
|
|
<!-- If Elif Else -->
|
|
|
|
<h3> If Elif Else </h3>
|
2018-11-15 20:09:21 +00:00
|
|
|
Sometimes, in programming, it is necessary to have a series of conditions that lead to
|
|
|
|
different paths. We can accommodate this need with the <i>if else elif</i> mechanism.
|
2018-11-14 13:36:20 +00:00
|
|
|
The <i>if else elif</i> mechanism takes this form:
|
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
if [condition]; then
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
elif [condition]; then
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
else
|
|
|
|
command1
|
|
|
|
command2
|
|
|
|
command3
|
|
|
|
...
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
2018-11-14 13:36:20 +00:00
|
|
|
|
|
|
|
Here is a simple example:
|
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
i=150;
|
|
|
|
|
|
|
|
if [$i -ge 200]; then
|
|
|
|
echo "You chose a big number."
|
|
|
|
elif [$i == 150]; then
|
|
|
|
echo "You chose 150".
|
|
|
|
else
|
|
|
|
echo "You chose a small number"
|
|
|
|
fi
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
In this example, which is just an extension of the previous example, we evaluate a
|
2018-11-15 15:00:14 +00:00
|
|
|
variable <code>i</code> to 150. If <code>i</code> is greater or equal to 200,
|
2018-11-14 21:15:38 +00:00
|
|
|
you print out "You chose a big number", if <code>i</code> is equal to 150 you print out
|
|
|
|
"You chose 150" otherwise you print out "You chose a small number".
|
2018-11-15 15:00:14 +00:00
|
|
|
Therefore, the output of this piece of code is:
|
2018-11-14 13:36:20 +00:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
You chose 150.
|
|
|
|
</pre>
|