Let’s continue to explore the next part of the shell script!
Arithmetic (continued)
(( ))
, the syntax:
1 2 |
$(( biểu thức )) |
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span class="token shebang important">#!/bin/bash</span> a <span class="token operator">=</span> <span class="token variable"><span class="token variable">$((</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">5</span> <span class="token variable">))</span></span> <span class="token comment">#line 3</span> <span class="token keyword">echo</span> <span class="token variable">$a</span> <span class="token comment">#10</span> a <span class="token operator">=</span> <span class="token variable"><span class="token variable">$((</span> <span class="token number">7</span> <span class="token operator">+</span> <span class="token number">5</span> <span class="token variable">))</span></span> <span class="token comment">#line 6</span> <span class="token keyword">echo</span> <span class="token variable">$a</span> <span class="token comment">#12</span> b <span class="token operator">=</span> <span class="token variable"><span class="token variable">$((</span> a <span class="token operator">+</span> <span class="token number">3</span> <span class="token variable">))</span></span> <span class="token comment">#line 9</span> <span class="token keyword">echo</span> <span class="token variable">$b</span> <span class="token comment">#15</span> b <span class="token operator">=</span> <span class="token variable"><span class="token variable">$((</span> $a <span class="token operator">+</span> <span class="token number">4</span> <span class="token variable">))</span></span> <span class="token comment">#line 12</span> <span class="token keyword">echo</span> <span class="token variable">$b</span> <span class="token comment">#16</span> <span class="token variable"><span class="token punctuation">((</span> b <span class="token operator">++</span> <span class="token punctuation">))</span></span> <span class="token comment">#line 15</span> <span class="token keyword">echo</span> <span class="token variable">$b</span> <span class="token comment">#17</span> <span class="token variable"><span class="token punctuation">((</span> b <span class="token operator">+</span> <span class="token operator">=</span> <span class="token number">3</span> <span class="token punctuation">))</span></span> <span class="token comment">#line 18</span> <span class="token keyword">echo</span> <span class="token variable">$b</span> <span class="token comment">#20</span> |
- Line 3 – This is the basic format, we can arrange it easily without quotes.
- Line 6 – It works the same way if we write without spaces.
- Line 9 – We can write including variables without the $ sign in front.
- Line 12 – Variables can be included with the $ sign if desired.
- Line 15 – Here the value of variable b is increased by 1. When we do this, we don’t need the $ sign before the parentheses.
- Line 18 – Here the value of variable b is increased by 3. It is a shorter spelling of
b = b + 3
.
1 2 |
${#variable} |
For example:
1 2 3 4 5 6 7 |
<span class="token shebang important">#!/bin/bash</span> a <span class="token operator">=</span> <span class="token string">'Hello'</span> <span class="token keyword">echo</span> <span class="token variable">${#a}</span> <span class="token comment"># 5</span> b <span class="token operator">=</span> 4567 <span class="token keyword">echo</span> <span class="token variable">${#b}</span> <span class="token comment"># 4</span> |
IF statement
if
- The
if
allow us to make decisions in our Bash scripts. It allows us to decide whether or not to run the code based on the conditions we can set.
- A basic if statement will check: if a specific condition returns true then it will perform a certain set of actions.If it will not perform those actions, the syntax is as follows:
1 2 3 4 5 |
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator"><</span> some condition <span class="token operator">></span> <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token operator"><</span> command <span class="token operator">></span> <span class="token keyword">fi</span> |
- Any statement between
then
andfi
will be executed if the condition returns true. - For example:
1 2 3 4 5 6 7 |
<span class="token shebang important">#!/bin/bash</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -gt 50 <span class="token punctuation">]</span> <span class="token comment">#line 3</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> Amazingggg. <span class="token comment">#line 5</span> <span class="token keyword">fi</span> |
- Line 3 – here, will check if the first input variable is greater than 50
- Line 5 – If the input variable is greater than 50, it will echo to the text screen “Amazingggg.”
1 2 3 4 |
➜ ~ ./test.sh 55 Amazingggg. ➜ ~ ./test.sh 2 |
Operator | Describe |
---|---|
! condition | If condition is true then returns false and vice versa |
-n string | string length is greater than zero |
-z string | string length is zero (ie empty) |
string1 = string2 | string1 is equal to string2 |
string1! = string2 | The two strings are not the same |
integer1 -eq integer2 | integer1 is equal to integer2 |
integer1 -gt integer2 | integer1 is greater than integer2 |
integer1 -lt integer2 | integer1 is smaller than integer2 |
-d file | The file exists and is a directory |
-e file | file exists |
-r file | The file exists and is readable |
-s file | The file exists and its size is bigger than 0 |
-w file | The file exists and is writable |
- Note:
=
slightly different from-eq
, for example[005 = 5]
will return false, and[005 -eq 5]
will return true.- When we mention the above
file
will be interpreted as apath
. Apath
– thepath
can be absolute or relative and can refer to a file or a directory. - We can use the command
test
to test cases we mention, for example:
1 2 3 4 5 6 7 |
➜ ~ <span class="token function">test</span> 005 <span class="token operator">=</span> 5 ➜ ~ <span class="token keyword">echo</span> <span class="token variable">$?</span> 1 <span class="token comment">#false</span> ➜ ~ <span class="token function">test</span> 005 -eq 5 ➜ ~ <span class="token keyword">echo</span> <span class="token variable">$?</span> 0 <span class="token comment">#true</span> |
- We can have multiple
if
in our script. For example:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="token shebang important">#!/bin/bash</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -gt 50 <span class="token punctuation">]</span> <span class="token comment">#line 3</span> <span class="token keyword">then</span> <span class="token keyword">if</span> <span class="token variable"><span class="token punctuation">((</span> $ <span class="token number">1</span> <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">))</span></span> <span class="token comment">#line 6</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> Congratulations. <span class="token comment">#line 8</span> <span class="token keyword">fi</span> <span class="token keyword">fi</span> |
- Line 3 – Executes when the first parameter passed is greater than 50
- Line 6 – If we want to test an expression we can use the opening and closing of the parentheses as above
- Line 8 – Will run if expression line 6 returns true
- In cases where we want to perform certain actions if the condition in
[]
returns true and others if false, we can write in the form:
1 2 3 4 5 6 7 |
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator"><</span> some condition <span class="token operator">></span> <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token operator"><</span> command <span class="token operator">></span> <span class="token keyword">else</span> <span class="token operator"><</span> other command <span class="token operator">></span> <span class="token keyword">fi</span> |
- For example:
1 2 3 4 5 6 7 8 9 |
<span class="token shebang important">#!/bin/bash</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -eq 1 <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> This Good <span class="token keyword">else</span> <span class="token keyword">echo</span> This Bad <span class="token keyword">fi</span> |
- It is possible to add multiple conditions that can lead to different results, by using
if elif else
. Syntax:
1 2 3 4 5 6 7 8 9 10 |
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator"><</span> some condition <span class="token operator">></span> <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token operator"><</span> command <span class="token operator">></span> <span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token operator"><</span> some condition <span class="token operator">></span> <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token operator"><</span> different command <span class="token operator">></span> <span class="token keyword">else</span> <span class="token operator"><</span> other command <span class="token operator">></span> <span class="token keyword">fi</span> |
- For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="token shebang important">#!/bin/bash</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -ge 20 <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> The Flower <span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> <span class="token operator">=</span> 10 <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> Good boy. <span class="token keyword">else</span> <span class="token keyword">echo</span> Thank you <span class="token keyword">fi</span> |
- You can have as many
elif
branches as you want. And the end will use theelse
option.
- This operator includes:
and - &&
andor- ||
- For example:
1 2 3 4 5 6 7 |
<span class="token shebang important">#!/bin/bash</span> <span class="token comment">#and_operator</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -ge 5 <span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token punctuation">[</span> <span class="token variable">$2</span> -le 10 <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> Correct. <span class="token keyword">fi</span> |
1 2 3 4 5 6 7 8 9 |
<span class="token shebang important">#!/bin/bash</span> <span class="token comment">#or_operator</span> <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$1</span> -ge 5 <span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token punctuation">[</span> <span class="token variable">$2</span> -le 10 <span class="token punctuation">]</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> Correct. <span class="token keyword">else</span> <span class="token keyword">echo</span> Bad. <span class="token keyword">fi</span> |
Case
statement
- Basically,
if
andcase
pretty similar. However, we often use thecase
in mul-ti choice + simple expressions, andif
will use for less-case problems and complex expressions. - Syntax of using
case
:
1 2 3 4 5 6 7 8 9 |
<span class="token keyword">case</span> <span class="token operator"><</span> variable <span class="token operator">></span> <span class="token keyword">in</span> <span class="token operator"><</span> value 1 <span class="token operator">></span> <span class="token punctuation">)</span> <span class="token operator"><</span> commands <span class="token operator">></span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> <span class="token operator"><</span> value 2 <span class="token operator">></span> <span class="token punctuation">)</span> <span class="token operator"><</span> other commands <span class="token operator">></span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> esac |
- For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="token shebang important">#!/bin/bash</span> <span class="token keyword">case</span> <span class="token variable">$1</span> <span class="token keyword">in</span> <span class="token comment">#line 3</span> yellow <span class="token punctuation">)</span> <span class="token comment">#line 4</span> <span class="token keyword">echo</span> This is yellow <span class="token punctuation">;</span> <span class="token punctuation">;</span> <span class="token comment">#line 6</span> red <span class="token punctuation">)</span> <span class="token keyword">echo</span> This is red <span class="token punctuation">;</span> <span class="token punctuation">;</span> black <span class="token punctuation">)</span> <span class="token keyword">echo</span> This is black <span class="token punctuation">;</span> <span class="token punctuation">;</span> * <span class="token punctuation">)</span> <span class="token comment">#line 13</span> <span class="token keyword">echo</span> Make color <span class="token punctuation">;</span> <span class="token punctuation">;</span> esac <span class="token comment">#line 16</span> |
- Line 3: This line starts executing
case
commands - Line 4: If the first passed variable
$1
isyellow
will execute the command inside it. The)
sign indicates the end of the case. - Line 6: We determine the end of this set of statements with a minus
;;
- Line 13: The
*
symbol represents any character, we can consider this to be the default if the variable passed in does not fall into any case. - Line 16:
esac
indicates that we are at the end of thecase
statement. Any other statements after it will be executed normally.