Shell Script – Continued (P3)

Tram Ho

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:

  • Results returned:

  • case $INPUT_STRING – here we check the value of the INPUT_STRING variable
  • The options are listed and followed by a sign ) eg 16) and 17) .
  • 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 message I don't understand is printed and the loop continues.
  • The entire case statement ends with the esac statement. Then we end the while loop with the done 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

  • The result is:

  • If we try to pass it 3 parameters, we will get the following result:

  • 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:

  • $# and $1 .. $9 are automatically set by shellscript.
  • We can get more than 9 parameters using the shift command:

  • For example:

  • 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 support let ).

  • For example:

– 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:

  • Alternatively, you can try with other operators like:


▪️ expr command

  • expr similar to let 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:

  • 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:


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

Share the news now

Source : Viblo