8.4 Conditional Branch



In programming, it is important to change what to do by its conditions. This is the conditional branch and exists in all programming languages. Of course, shell scripts also have it.

8.4.1 if sentence

'if' sentence is the most typical statement of conditional branch. The usage is as follows.

Format

if conditional_expression_1 then ... elif conditional_expression_2 ... else ... fi


We can omit the portion of 'fi elif...' and the portion of 'else...'. ‘elif ‘ is used to judge on another conditions (conditional_expression_2).

‘else’ is performed when all conditions are not applied. 'if' sentence is ended by 'fi'. This system of "ending by the inversion of the command which began the sentence" also appears in other grammar parts.


Followings are some usages for conditional expressions in 'if' sentence.


 Operand  How to judge
 a = b  If a is equivalent to b, the condition is truth
 a != b  If a is not equivalent to b, the condition is truth

Table 8-1: String comparison operand

 Operand  How to judge
 a -eq b  If a is equal to b, the condition is truth
 a -ne b  If a is not equal to b, the condition is truth
 a -ge b  If a is greater or equal to b, the condition is truth
 a -le b  If a is less or equal to b, the condition is truth
 a -gt b  If a is greater than b, the condition is truth
 a -lt b  If a is less than b, the condition is truth

Table 8-2: Numeric comparison operand

The file attribute comparison operands are used as follows.


Format

if [ -d path ]; then.....


The portion of -d is the attribute comparison operand of a file. -d judges whether specified path is a directory. If the path is a directory, the condition is truth.


A conditional clause can be used by using test command. 'if' sentence which was written above can be rewritten as follows.


 Operand  What is checked
 -f  If the path exists and is a file, the condition is truth
 -d  If the path exists and is a directory, the condition is truth
 -e  If the path exists, the condition is truth
 -x  If the path exists and it is executable for its shell script executing user, the condition is truth
 -L  If the path exists and it is symbolic link, the condition is truth

Table 8-3: File attribute comparison operand

Format

if [ conditional_clause ]; then ...
if test conditional_clause ; then ....


As for both, the meanings and result are same.




Column: The parenthesis is a command


'if' sentence using test command was introduced. Can you recognize that both '[' and 'test' are at the same place? Both have almost same function, and they require conditional clause next. '[' is a command and executable as test is so. If you execute “which [“, you can get installed path.




In the case of conditional branch, two or more conditions can be piled up. When Conditions A and Conditions B need to be satisfied simultaneously, the logical multiplication(=AND) is used. When at least one of Condition A or Condition B needs to be satisfied, the logical sum(=OR) is used.

There are two ways of writing the logical multiplication and the logical sum in shell script.


We can use either '-a' or '&&'. Each usage is as follows.


Format

[Condition A -a Condition B -a Condition C ] ....
[Condition A] && [Condition B] && [Condition C] ...


Please notice that there are small differences about how to put [].


We can use either '-o' or '||'. Each usage is as follows.


Format

[Condition A -o Condition B -o Condition C ] ....
[Condition A] || [Condition B] || [Condition C] ...


8.4.2 Multiway Branch

Let's talk about multiway branch. If we use ‘if’ sentence , it will be as follows.


if conditional_expression_1 then
      :
elif conditional_expression_2 then
      :
elif conditional_expression_3 then
      :
      :
fi


In shell script, the case sentence is prepared for multiway branch.


Format

case variable in
      value A)
            processing_1;;
      value B)
            processing_2;;
esac


When the variable is A, processing 1 is performed. The case sentence ends by 'esac' which is description from the contrary of a 'case'.

We can put plural values by separating with pipe(|).


Example

$ cat case.sh ↵             case.sh is created and performed as follows.
#!/bin/bash

case $1 in
      a|A)
            echo " a or A was inputted into the argument ";;
      b|B)
            echo " b or B was inputted into the argument ";;
esac
$ ./case.sh a ↵
a or A was inputted into the argument
$ ./case.sh B ↵
b or B was inputted into the argument


Moreover, a asterisk (*) is used for processing when matching nothing.

Example

$ cat defaultcase.sh ↵ ;defaultcase.sh is created and performed as follows.
#!/bin/bash

case $1 in
      1)
            echo " 1 was inputted into the argument ";;
      2)
            echo " 2 was inputted into the argument ";;
      *)
            echo " Something except 1 and 2 was inputted ";;
esac
$ ./defaultcase.sh 1 ↵
1 was inputted into the argument
$ ./defaultcase.sh 2 ↵
2 was inputted into the argument
$ ./defaultcase.sh 0 ↵
Something except 1 and 2 was inputted
(When the variable doesn't match to all candidates, processing of value * is performed.)



Previous Next