Published on March 6, 2017 by ProgrammingKnowledge

Conditionals let us decide whether to perform an action or not, this decision is taken by evaluating an expression.

Expressions
An expression can be: String comparison, Numeric comparison, File operators and Logical operators and it is represented by [expression]:
String Comparisons:
———————————
= compare if two strings are equal
!= compare if two strings are not equal
-n evaluate if string length is greater than zero
-z evaluate if string length is equal to zero

Examples:
[ s1 = s2 ] (true if s1 same as s2, else false)
[ s1 != s2 ] (true if s1 not same as s2, else false)
[ s1 ] (true if s1 is not empty, else false)
[ -n s1 ] (true if s1 has a length greater then 0, else false)
[ -z s2 ] (true if s2 has a length of 0, otherwise false)

Number Comparisons:
————————————
-eq compare if two numbers are equal
-ge compare if one number is greater than or equal to a number
-le compare if one number is less than or equal to a number
-ne compare if two numbers are not equal
-gt compare if one number is greater than another number
-lt compare if one number is less than another number

Examples:
[ n1 -eq n2 ] (true if n1 same as n2, else false)
[ n1 -ge n2 ] (true if n1greater then or equal to n2, else false)
[ n1 -le n2 ] (true if n1 less then or equal to n2, else false)
[ n1 -ne n2 ] (true if n1 is not same as n2, else false)
[ n1 -gt n2 ] (true if n1 greater then n2, else false)
[ n1 -lt n2 ] (true if n1 less then n2, else false)

Leave a Reply

8 Comments on "Shell Scripting Tutorial for Beginners 5 – If Statement ( If then , If then else, If elif else)"

Notify of
avatar

Suneel Maurya
Guest
Suneel Maurya
1 month 13 days ago

I am trying to print this #! /bin/bashecho $0 $1 $2 $3 ' > $1 $2 $3'but getting error,,,like this bash: ./suneel.sh: Permission denied……….why its happening?? Please suggest on it…

Daniel O'Farrell
Guest
Daniel O'Farrell
1 month 15 days ago
#! /bin/bashecho -e "Enter the name of the file : c"read file_nameif [ -f $file_name ]then echo "E_File = True" echo "Would you like to change the permissions of this file?" read YorN1 if [ $YorN1 = "y" ] then chmod -w $file_name echo "Permissions change = True" elif [$YorN1 = "n"] echo "Would you like to change this files permissions back?" read YorN2 if [$YorN2 = "y"] then chmod +w $file_name echo "Permissions change = True" elif [$YorN2 = "n"] echo "Permissions change = False" fi fielif [$YorN1 = "n"]fii keep getting the same error that its an unexpected… Read more »
Daniel O'Farrell
Guest
Daniel O'Farrell
1 month 14 days ago

Mike Adajar around the fi? And I got a different version of this code running and it works sort of, I just need to find a way to lock them from changing the permissions back

Mike Adajar
Guest
Mike Adajar
1 month 14 days ago

Daniel O'Farrell double bracket?

Rafik Dzfire
Guest
Rafik Dzfire
2 months 12 days ago

idiot ,this is not tutorial ,this is presentation of shell bash

ProgrammingKnowledge
Guest
ProgrammingKnowledge
4 months 16 days ago

For more detailed Tutorial visit – goo.gl/vn9xIb

ZnXii
Guest
ZnXii
4 months 21 days ago

poo in the loo

EM 360 Technology
Guest
EM 360 Technology
4 months 21 days ago

nice

wpDiscuz