Let’s continue to explore the next part of the shell script!
Case in shellscript
The case
statement works the same way as if .. then .. else
. Its syntax is quite simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">echo</span> <span class="token string">"How old are you?"</span> <span class="token keyword">while</span> <span class="token keyword">:</span> <span class="token keyword">do</span> <span class="token function">read</span> INPUT_STRING <span class="token keyword">case</span> <span class="token variable">$INPUT_STRING</span> <span class="token keyword">in</span> 16 <span class="token punctuation">)</span> <span class="token keyword">echo</span> <span class="token string">"Wowwwwwwwww"</span> <span class="token keyword">break</span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> 17 <span class="token punctuation">)</span> <span class="token keyword">echo</span> <span class="token string">"Yoooooooo!!!"</span> <span class="token keyword">break</span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> * <span class="token punctuation">)</span> <span class="token keyword">echo</span> <span class="token string">"I don't understand"</span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> esac <span class="token keyword">done</span> <span class="token keyword">echo</span> <span class="token keyword">echo</span> <span class="token string">"Thank you so much!"</span> |
- Results returned:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | How old are you? 16 Wowwwwwwwww Thank you so much <span class="token operator">!</span> ➜ ~ ./var_exam.sh How old are you? 17 Yoooooooo <span class="token operator">!</span> <span class="token operator">!</span> <span class="token operator">!</span> Thank you so much <span class="token operator">!</span> ➜ ~ ./var_exam.sh How old are you? gdg I don't understand |
case $INPUT_STRING
– here we check the value of theINPUT_STRING
variable- The options are listed and followed by a sign
)
eg16)
and17)
. - This means that if
INPUT_STRING
matches 16, the code inside that case will be executed, until the;;
sign;;
- If INPUT_STRING does not match case 16 or 17, it will fall into the other case (in the example above
*)
) the messageI don't understand
is printed and the loop continues. - The entire
case
statement ends with theesac
statement. Then we end the while loop with thedone
statement.
Variables in shellscript (Part 2)
- Variable group
$0..$9
and$#
. $0
– Name of the Bash script.$1- $9
– the first 9 arguments for the Bash script. (as mentioned above)$#
– How many arguments passed to the script Bash.[email protected]
– All arguments provided for the Bash script.$?
– Exit status of the most recent running procedure.$$
– Process ID of the current script$USER
– The username of the user who is running the script.$HOSTNAME
– The hostname of the machine on which the script is running.$SECONDS
– The number of seconds since the script was started.$RANDOM
– Returns a different random number each time mentioned.$LINENO
– Returns the current line number in the Bash script.- We will go into the example to better understand offline
1 2 3 4 5 6 7 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">echo</span> <span class="token string">"Hello! I have <span class="token variable">$#</span> parameters"</span> <span class="token keyword">echo</span> <span class="token string">"My file name is <span class="token variable">$0</span> "</span> <span class="token keyword">echo</span> <span class="token string">"First parameter is <span class="token variable">$1</span> "</span> <span class="token keyword">echo</span> <span class="token string">"Second parameter is <span class="token variable">$2</span> "</span> <span class="token keyword">echo</span> <span class="token string">"All parameters are <span class="token variable"><a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="250165">[email protected]</a></span> "</span> |
- The result is:
1 2 3 4 5 6 7 | ➜ ~ ./variables.sh Hello! I have 0 parameters My file name is ./variables.sh First parameter is Second parameter is All parameters are |
- If we try to pass it 3 parameters, we will get the following result:
1 2 3 4 5 6 7 | ➜ ~ ./variables.sh test hello bye Hello! I have 3 parameters My file name is ./variables.sh First parameter is test Second parameter is hello All parameters are test hello bye |
- Note that the value of
$0
varies depending on how the script is called. To show the output file name on screen we can use:
1 2 | <span class="token keyword">echo</span> <span class="token string">"My file name is <span class="token variable"><span class="token variable">`</span> <span class="token function">basename</span> $0 <span class="token variable">`</span></span> "</span> |
$#
and$1 .. $9
are automatically set by shellscript.- We can get more than 9 parameters using the
shift
command:
1 2 3 4 5 6 7 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token string">" <span class="token variable">$#</span> "</span> -gt <span class="token string">"0"</span> <span class="token punctuation">]</span> <span class="token keyword">do</span> <span class="token keyword">echo</span> <span class="token string">" <span class="token variable">$1</span> is <span class="token variable">$1</span> "</span> <span class="token function">shift</span> <span class="token keyword">done</span> |
- For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | ➜ ~ ./variables.sh 1 2 3 4 5 6 7 8 9 10 11 More parameter is 1 More parameter is 2 More parameter is 3 More parameter is 4 More parameter is 5 More parameter is 6 More parameter is 7 More parameter is 8 More parameter is 9 More parameter is 10 More parameter is 11 |
- The above script uses
shift
until$#
decreases to 0.
Arithmetic (Arithmetic)
The let
statement
let
is a Bash built-in function that allows us to perform simple arithmetic. It follows the basic format (note here we will use#!/bin/bash
instead of#!/bin/sh
since it doesn’t supportlet
).
1 2 | <span class="token keyword">let</span> <span class="token operator"><</span> arithmetic expression <span class="token operator">></span> |
- 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">let</span> number <span class="token operator">=</span> 5+5 <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment">#10</span> <span class="token keyword">let</span> <span class="token string">"number = 5 + 5"</span> <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment">#10</span> <span class="token keyword">let</span> number++ <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment">#11</span> <span class="token keyword">let</span> <span class="token string">"number = 5 * 5"</span> <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment">#25</span> <span class="token keyword">let</span> <span class="token string">"number = <span class="token variable">$1</span> + 30"</span> <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment">#30 + first command line argument</span> |
– In the above example:
- Line 4 – This is the basic format. Note that if we don’t put quotes around the expression, it must be written without spaces.
- Line 7 – This time we have used citations that allow us to remove the expression to make it more readable.
- Line 10 – This is a way to increase the value of the variable
number
to 1. It’s like writing “number = number + 1”. - Line 16 – We can also include other variables in the expression.
- And the result returns:
1 2 3 4 5 6 7 | ➜ ~ ./arithmetic.sh 5 10 10 11 25 35 |
- Alternatively, you can try with other operators like:
1 2 3 4 5 6 7 8 9 | +, -, *, / <span class="token comment">#cộng, trừ, nhân, chia</span> var ++ <span class="token comment">#Tăng biến var thêm 1</span> var-- <span class="token comment">#Giảm var biến 1</span> % <span class="token comment">#Trả lại phần còn lại sau khi chia</span> |
expr
command
1 2 | <span class="token function">expr</span> item1 operator item2 |
expr
similar tolet
but instead of saving the result to a variable, it will print the answer off the screen as well.- Unlike
let
, it doesn’t need to enclose expressions in quotes. - We also need to have spaces between the items in the expression.
- For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token shebang important">#!/bin/bash</span> <span class="token function">expr</span> 5 + 5 <span class="token function">expr</span> <span class="token string">"5 + 5"</span> <span class="token function">expr</span> 5+5 <span class="token function">expr</span> 5 * <span class="token variable">$1</span> <span class="token function">expr</span> 13 % 2 number <span class="token operator">=</span> <span class="token variable"><span class="token variable">$(</span> <span class="token function">expr</span> 10 - 3 <span class="token variable">)</span></span> <span class="token keyword">echo</span> <span class="token variable">$number</span> <span class="token comment"># 7</span> |
- Line 4 – This is the basic format. Note that there must be spaces between items and no quotes.
- Line 6 – If we put quotation marks around the expression then the expression would not be executed but instead printed.
- Line 8 – If we do not put spaces between the entries of the expression then the expression will not be executed but instead printed.
- Line 10 – Some characters have special meanings with Bash, so we have to escape them (put backslashes first) to eliminate their special meanings.
- Line 12 – Here we allow us to print out the remainder of the above division.
- Line 14 – This time we use expr in the alternate command to save the result to the variable
number
. - Results returned:
1 2 3 4 5 6 7 8 | ➜ ~ ./arithmetic.sh 5 10 5 + 5 5+5 25 1 7 |
This article, maybe I will end here
In the following article, I will continue to study more about Arithmetic or Function, … of Shell Script!
Thank you for reading my article