-->

Conditional Statements if and else in Shell Scripting

Conditional statements if and else in scripting Linux


if statement
If the given Statement is true then it executes the result.else it shows as false.
its helps to control specific decisions.but the syntax is quite different with c language and c++ 
let's check out some examples.

Else Statement
if your statement is wrong then it doesn't execute internal code.if the given Statement is true then it executes the result.



#!/bin/bashnum=143if [ $num -gt 100 ] thenecho "number $num is greater"else echo "number is smaller"fi

By using above code let's check whether 143 is greater than 100 or not.Where above statement is correct prints true as result

Rules to create if statements
1)There must be spaces while starting square brackets and ending square bracket [ $num -gt 100 ] its a valid statement

2) if there is no spaces while starting square brackets and ending square bracket is a invalid statement [$num -gt 100]

3) you have to use then keyword along with if.when if condition is true then the positive result will be shown. Whatever you write after then keyword gets executed when given Statement is true.

4) fi keyword is commonly indicates the last line of the if condition.basically the code between then and fi gets executed when given statement is true.fi is ending tag for if statement.

Operators of Shell Scripting

-eq equal to.
-gt greater than.
-ge greater than and equal to.
-le less than and equal to.
-lt less than.
-ne not equal


Use operators with Scripts

-eq equal to



#!/bin/bashecho "enter first number"read num1echo "enter second number"read num2if [ $num1 -eq $num2 ] thenecho "numbers are equals"fi 

-gt greater than




#!/bin/bash echo "enter first number"read num1echo "enter second number"read num2if [ $num1 -gt $num2 ] thenecho "$num1 is greater"elseecho "$num2 is greater"fi

-lt less than 



#!/bin/bash 
echo "enter first number"read num1echo "enter second number"read num2if [ $num1 -lt $num2 ] thenecho "$num1 is lesseeer"elseecho "$num2 is lesser"fi

also try for other operators

check Whether File Exists or Not

Now let's check whether file is available in directory or not
paste below code in linux terminal hit enter


#!/bin/bashecho "enter a file name"read file if [ -f $file ] then echo "file exist"else echo "file does not exist"fi

-f it tells shell to find file in your current directory

let's search for directory or folder

copy below code and paste code in linux terminal hit enter

#!/bin/bashecho "enter a file name"read file if [ -d $file ] then echo "file exist"else echo "file does not exist"fi

search files in specific directory

you have to give full path like exSdcard/Android/data/obb/com.tencent.ig shows pubg obb folder

#!/bin/bashecho "enter a full path of file"read file if [ -f $file ] then echo "file exist"else echo "file does not exist"fi


conclusion


I hope you may loved this cool post.If you have any doubts post in comment section.
Please check out advertisements on our website which helps us to post more content for you .
 Don't forget to share this cool post with all your friends and groupspread. HAVE  A  GREAT  DAY. 

Also Read

Post a Comment