If-Elif operators are used to designing the flow of the bash script. We can change the flow according to given conditions with
If-Elif . If-Else is one of the most used keywords or structures in Bash in order to make a different execution path according to the situation. For example, if given process ID or programs executes kill this process or rerun the process etc. logic can be implemented with the If-Else conditionals.
If-Else syntax is dynamic where a single condition or multiple conditions can be checked with it. Below we will provide a generic If-Else syntax for single or multiple conditions.
Single Condition If-Else
The most basic and easy implementation of the If-Else is single condition If-Else. We will just use the
fi keywords by provides the CONDITION and CODE_BLOCK. Below you can see the syntax. If the CONDITION_1 returns true or 1 the CODE_BLOCK_1 will be executed.
if CONDITION_1; then CODE_BLOCK_1 fi
Below we will make a very simple example related to the single condition if-else. We will set the age variable value and check if the age is above the 20 and print some message to the screen.
age=25 if [ $age -gt 20 ]; then echo "You age is above 20" fi
Two Condition If-Else
Two conditions if-else is another popular use case. We can specify two conditions where the second condition is reverse or not of the first condition. For example, if your age is above 20 bla bla bla, if not bla bla bla. In this syntax we do not have to specify the second condition explicitly but if we need we can use the Multiple Condition If-Else.
if CONDITION_1; then CODE_BLOCK_1 else CODE_BLOCK_2 fi
Below example we will check given age and print messages for above the 20 and equal to or below 20.
age=25 if [ $age -gt 20 ]; then echo "You age is above 20" else echo "You age is equal or below 20" fi
Multiple Condition If-Else
The most dynamic and usefull type of if-else is multiple condition if-else. We can specify two or more conditions to meet. There is not limit the count of condition like we can specify 10 or 100 conditions by using the
if CONDITION_1; then CODE_BLOCK_1 elif CONDITION_2; then CODE_BLOCK_2 elif CONDITION_3; then CODE_BLOCK_3 ... else CODE_BLOCK_4 fi
In the following examle we will check if the given age is above 20, below 5 or non of them and print message accordingly.
age=25 if [ $age -gt 20 ]; then echo "You age is above 20" elif [ $age -lt 5 ]; then echo "Your age is lower than 5" else echo "You age is between 5 and 20" fi
When writing scripts we have a lot of situations to make decisions. We solve the questions marks of the scripts with conditionals. For example, the simplest one is we want to run a script if a directory or file exists.
if [ Clean.sh ] then echo "Clean.sh exist" fi
Clean.sh exists then echo
The most used and simple conditional is if/then/else statement. The statement starts with if and lists conditions and closed with fi. Here [ … ] brackets look for the file and provide logic for if. Do not be afraid there is a lot of functions to test a lot of things. We will touch some of them.
if grep -q tmp Clean.sh then echo "File have temp string" fi
File Test Operators
While scripting there will be a lot of file interactions. Bash provides a lot of options for file testing. For example to check if a file exists to use the following statement
file="Clean.sh" if [ -e $file ] then echo "Clean.sh exists" fi
In order to compare two or more variables or values comparison operators are used. In this example is the count is greater than 5 below example is used. The greater than operator is
count=6 if [ $count -gt 5 ] then echo "count is greater then 5" fi
Condition Check Operators
Like and programming language Bash if-else provides different condition check operators which can be used for different cases like check file existence, number comparison, file type etc. These condition check operators can be categorized as
File Conditions ,
Number Conditions ,
File conditions provide following checks.
|-e||Checks if the file exists or not|
|-d||Checks if the file is a directory|
|-b||Checks if the file is a block device|
|-c||Checks if the file is a character device|
|file1 -nt file2||If file file1 is newer than file file2|
|file1 -ot file2||If file file1 is older than file file2|
|-r||File can be read (read permission)|
|-w||File can be modified (write permission)|
|-x||File can be executed (execute permission)|
Number conditions provide following checks.
|num1 -eq num2||Check if numbers are equal|
|num1 -ne num2||Check if numbers are not equal|
|num1 -lt num2||Checking if num1 is lower than num2|
|num1 -le num2||Lower or equal than num2|
|num1 -gt num2||Greater than num2|
|num1 -ge num2||Greater or equal than num2|
String conditions provide following checks.
|str1 = str2||Checks if strings are equal or not|
|str1 != str2||Checks if strings are different|
|-z str1||Checks if str1 is empty|
|-n str1||Checks if str1 is not empty|