In the previous post, we learned how to use variables in shell script. Next, we are going to learn about using if-else in shell scripting. The if-else construct is used whenever we want to evaluate a condition i.e., whether some condition is true or false.
Syntax
if [ condition ] then statement 1 statement 2 … statement n else statement 1 statement 2 … statement n fi
if the condition is true then the statements within the “then” and “else” are executed and if the condition is false then the statements within the else and fi are executed. The fi acts as a termination symbol.
Note: Make sure you type the syntax correctly. Give space on either side of ‘[‘ and before ‘]’
Script 1: Check if the number entered by the user is greater than 10
echo "Enter a number" read num if [ $num -gt 10 ] then echo "Number is greater than 10" else echo "Number is less than 10" fi
Output
$./script1
Enter a number
12
Number is greater than 10
Operators
The various operators used in shell scripting are as below:
Arithmetic Operators
Comparison | Expression (exp) |
Equal to | exp1 –eq exp2 |
Not equal to | exp1 –ne exp2 |
Greater than | exp1 –gt exp2 |
Greater than or equal to | exp1 –ge exp2 |
Less than | exp1 –lt exp2 |
Less than or equal to | exp1 –le exp2 |
If expression is false | ! exp |
String Comparison
Comparison | Expression |
True If strings are equal | String1 = String2 |
True If strings are not equal | String1 != String2 |
True If string is not null | -n string |
True if the string is null | -z string |
File Conditions
Condition | Result |
-d file | True if the file is a directory. |
-e file | True if the file exists |
-f file | True if the file is a regular file |
-r file | True if the file is readable |
-s file | True if the file has non-zero size |
-w file | True if the file is writable |
-x file | True if the file is executable |
Practice Questions
Q1. Write a script to check if a file exists. If not, then create it.
Solution:
echo "Enter the name of file" read file if [ -f $file ] then echo "file exits" else touch $file echo "file created" fi echo "End of if statement"
Q2. Write a shell script which checks the total number of arguments passed. If the argument count is greater than 5 display the message “Too many arguments”
Solution:
echo "Total number of arguments are: $#" if [ $# -gt 5 ] then echo "Too many arguments" else echo "Less arguments" fi
The elif Construct
Sometimes multiple conditions are required to be evaluated, then we use the elif construct.
Syntax
if [ condition1 ] then statements elif [ condition2 ] then statements else statements fi
Example: Write a script that checks if the argument passed at command line is whether of a file or a directory.
if [ -f $1 ] then echo "file exits" elif [ -d $1 ] then echo "its a directory" else echo "The argument is neither a file name nor a directory name" fi
Video
Practice Questions based on using if-else in shell scripting
Q1. Write a shell script to read a month name from the user. Check if the name entered is either August or October
Q2. Write a script that takes two command line arguments. Check whether the name passed as first argument is of a directory or not. If not, then create a new directory with the name passed as second argument
Previous Next Variables for loop