/**/ Using if-else in Shell Scripting - Dextutor Dextutor

Using if-else in Shell Scripting

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

ComparisonExpression (exp)
Equal toexp1 –eq exp2
Not equal toexp1 –ne exp2
Greater thanexp1 –gt exp2
Greater than or equal toexp1 –ge exp2
Less thanexp1 –lt exp2
Less than or equal toexp1 –le exp2
If expression is false! exp

String Comparison

ComparisonExpression
True If strings are equalString1 = String2
True If strings are not equalString1 != String2
True If string is not null-n string
True if the string is null-z string

File Conditions

ConditionResult
-d fileTrue if the file is a directory.
-e fileTrue if the file exists
-f fileTrue if the file is a regular file
-r fileTrue if the file is readable
-s fileTrue if the file has non-zero size
-w fileTrue if the file is writable
-x fileTrue 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"
using if-else in shell scripting

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

using if-else in shell scripting

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

Leave a Comment