ShellScript – Continued (P4)

Tram Ho

Let’s continue to explore the next part of the shell script!
🔻 Arithmetic (continued)

▪️ Using double parentheses (( )) , the syntax:

For example:

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

▪️ The length of a variable, the syntax:

For example:


🔻 IF statement

▪️ We will learn more about 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.

▪️ Basic IF statements

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

  • Any statement between then and fi will be executed if the condition returns true.
  • For example:

  • 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.”


▪️ Operator

OperatorDescribe
! conditionIf condition is true then returns false and vice versa
-n stringstring length is greater than zero
-z stringstring length is zero (ie empty)
string1 = string2string1 is equal to string2
string1! = string2The two strings are not the same
integer1 -eq integer2integer1 is equal to integer2
integer1 -gt integer2integer1 is greater than integer2
integer1 -lt integer2integer1 is smaller than integer2
-d fileThe file exists and is a directory
-e filefile exists
-r fileThe file exists and is readable
-s fileThe file exists and its size is bigger than 0
-w fileThe 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 a path . A path – the path 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:


▪️ Nested if statement

  • We can have multiple if in our script. For example:

  • 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

▪️ If else statement

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

  • For example:

▪️ If elif else statement

  • It is possible to add multiple conditions that can lead to different results, by using if elif else . Syntax:

  • For example:

  • You can have as many elif branches as you want. And the end will use the else option.

▪️ Boolean operator

  • This operator includes: and - && and or- ||
  • For example:


▪️ Repeat the Case statement

  • Basically, if and case pretty similar. However, we often use the case in mul-ti choice + simple expressions, and if will use for less-case problems and complex expressions.
  • Syntax of using case :

  • For example:

  • Line 3: This line starts executing case commands
  • Line 4: If the first passed variable $1 is yellow 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 the case statement. Any other statements after it will be executed normally.

Above I have learned more about Arithmetic and If Else . I would like to finish part 4 here.
Thank you for reading this article!
Share the news now

Source : Viblo