Conditional structures
If-then-else and elif
Conditional expression verify if a statement is true or false. If the statement is true, the code inside the if block is executed. If the statement is false, the code inside the else block is executed. In the first example only the if block is executed when the condition is true.
1if [ $a -eq $b ]; then
2 echo "a is equal to b"
3fi
Beside executing the code inside the block if the statement is true, the code inside the else block is executed when the condition is false.
1if [ $a -eq $b ]; then
2 echo "a is equal to b"
3else
4 echo "a is not equal to b"
5fi
If the condition is false, the code inside the else block is executed. The else block can also be used as a second if-then-else
structure by using the elif
keyword.
1if [ $a -eq $b ]; then
2 echo "a is equal to b"
3elif [ $a -eq $c ]; then
4 echo "a is not equal to b"
5else
6 echo "a is not equal to b or c"
7fi
Test Operators
For conditional structures to function, the condition must be expressed in a form that can be evaluated by the shell. The command test
is used to evaluate the condition and the short form is the [
sign.
File system tests
The test command has numerous options to test the existence of files and directories as in the following example. After the test command, the expression is evaluated as a boolean expression. So with the option -e
, the expression is evaluated as a file existence test, but no other test is performed.
1if [ -e filename ]; then
2 echo "file with name `filename` exists"
3fi
As everything on a Unix file system is a file, the test command can be used to test the existence of files and directories. To be more explicit, the test command can be used to test if a regular file for example. With the option -f
, the expression is evaluated as a file existence test and the test is performed on the file type.
1if [ -f filename ]; then
2 echo "file exists and is a regular FILE"
3fi
The same can be done with the test command to test if a directory exists. The example below tests if the directory dirname
exists and if the directory doesn’t exist, the directory is created.
1if [ ! -d dirname ]; then
2 mkdir dirname
3fi
The previous examples only showed a couple of cases, but the table below shows all the available options and their meaning. Understanding the direct options is important to understand the meaning of the test command and write the correct conditional expression.
Operator |
Description |
---|---|
|
FILE1 and FILE2 have the same device and inode numbers |
|
FILE1 is newer (modification date) than FILE2 |
|
FILE1 is older than FILE2 |
|
Test if FILE exists |
|
Test if FILE exists and is block special |
|
Test if FILE exists and is character special |
|
Test if FILE exists |
|
Test if FILE exists and is a regular file |
|
Test if FILE exists and is set-group-ID |
|
Test if FILE exists and is owned by the effective group ID |
|
Test if FILE exists and is a symbolic link |
|
Test if FILE exists and has its sticky bit set |
|
Test if FILE exists and is a symbolic link |
|
Test if FILE exists and has been modified since it was last read |
|
Test if FILE exists and is a directory |
|
Test if FILE exists and is a named pipe |
|
Test if FILE exists and a read permission is granted |
|
Test if FILE exists and has a size greater than zero |
|
Test if FILE exists and is a socket |
|
Test if file descriptor FD is opened on a terminal |
|
Test if FILE exists and its set-user-ID bit is set |
|
Test if FILE exists and write permission is granted |
|
Test if FILE exists and execute (or search) permission is granted |
String comparison
1if [ -n variable ]; then
2 echo "variable is not empty"
3fi
4if [ variable ]; then
5 echo "variable is not empty"
6fi
1if [ -z variable ]; then
2 echo "variable is empty"
3fi
1if [ variable1 = variables ]; then
2 echo "variable1 is equal to variable2"
3fi
1if [ variable1 != variables ]; then
2 echo "variable is not equal to variable2"
3fi
Integer comparison
1if [ variable1 -eq variables ]; then
2 echo "variable1 is equal to variable2"
3fi
1if [ variable1 -ne variables ]; then
2 echo "variable1 is not equal to variable2"
3fi
1if [ variable1 -ge variables ]; then
2 echo "variable1 is greater than or equal to variable2"
3fi
4if [ variable1 -gt variables ]; then
5 echo "variable1 is greater than variable2"
6fi
7if [ variable1 -le variables ]; then
8 echo "variable1 is less or equal to variable2"
9fi
10if [ variable1 -lt variables ]; then
11 echo "variable1 is less than variable2"
12fi
Combined comparison
and
test operators1if [ -d dirname -a -g dirname ]; then
2 echo "directory exists"
3fi
or
test operators1if [ -d dirname -o -g dirname ]; then
2 echo "directory exists"
3fi
Case
The case statement is used to test a value and execute a statement depending on the value. It is an alternative to the elif
statement and easier to write and read.
1read -p "Enter a number: " a
2case $a in
3 1)
4 echo "a is 1"
5 ;;
6 2)
7 echo "a is 2"
8 ;;
9 3)
10 echo "a is 3"
11 ;;
12 *)
13 echo "a is not 1, 2 or 3"
14 ;;
15esac
Select
With control structures, it is possible to select one of several possible actions depending on the value of a variable. The select statement is used to do this as it is a way to create a menu in a controlled way. The select statement is similar to the case statement, but it is more flexible. The select statement is used to test a value and execute a statement depending on the value. In th example below a case statement is executed when the selected value is correct.
1PS3="Enter your choice: "
2select opt in jack john jane quit
3do
4 case $opt in
5 jack)
6 echo "The chosen one is Jack"
7 ;;
8 john)
9 echo "The chosen one is John"
10 ;;
11 jane)
12 echo "The chosen one is Jane"
13 ;;
14 quit)
15 echo "Goodbye"
16 break
17 ;;
18 *)
19 echo "Invalid option $REPLY"
20 ;;
21 esac
22done