Followers

Linux Shell Script Decision making Statements

  These are the statements which redirect the logic of a script on the basis of some condition. If If the condition is satisfied then if blo...

 These are the statements which redirect the logic of a script on the basis of some condition.

If

If the condition is satisfied then if block gets executed otherwise if statements will not execute

Example

Find the total free space on the root directory and if it is less than 20% (free/total *100) then send a notification message then "Alert! disk space is too low"

usedspace=`df / | awk '{print $5}' | sed '1d' | cut -c1`
threshold=2
if [ $usedspace -gt $threshold ]
then
        echo "Alert ! used space $usedspace which is more than $threshold"
fi

Example with command-line value for the threshold

usedspace=`df / | awk '{print $5}' | sed '1d' | cut -c1`
threshold=$1
if [ $usedspace -gt $threshold ]
then
        echo "Alert ! used space $usedspace which is more than $threshold"
fi

Execute

./alert.sh 80

If - else

usedspace=`df / | awk '{print $5}' | sed '1d' | cut -c1`
threshold=$1
if [ $usedspace -gt $threshold ]
then
        echo "Alert ! used space $usedspace which is more than $threshold"
else
        echo "You have sufficient free space :)"
fi

Execute

./alert.sh 80

If -elif 

For multiple if conditions

Example 

Perform certain operation on apache service

status=`systemctl status apache2 | grep "active (running)" | wc -l`
if [ $# -lt 1 ]
then
        var1="restart"
        echo "No commandline argument is passed so default functionality is restarted"
else
       var1="$1"
fi
echo "-----------Checking Apache Status------------"
if [ $status -ge 1 ] && [  $var1 == 'stop' ]
then
        echo "apache is running now it is getting stopped"
        systemctl stop apache2
        echo "apache is in stopped mode"
elif [ $status -ge 1 ] && [  $var1 == 'restart' ]
then
       echo "apache is running but the request is to restart it so restarting the apache"
       systemctl restart apache2
       echo "apache is in started mode"
elif [ $status -lt 1 ] && [  $var1 == 'restart' ]
then
       echo "current status of apache is in stopped mode so as per request restarting the apache"
       systemctl restart apache2
             echo "apache is in started mode"
fi
echo "-------------Apache Operations are done---------"





Execute

./apachestatus.sh

output

No commandline argument is passed so default functionality is restarted

-----------Checking Apache Status------------

apache is running but the request is to restart it so restarting the apache

apache is in started mode

-------------Apache Operations are done---------

Execute

 ./apachestatus.sh stop

output

-----------Checking Apache Status------------

apache is running now it is getting stopped

apache is in stopped mode

-------------Apache Operations are done---------




case..esac

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Linux Shell Script Decision making Statements
Linux Shell Script Decision making Statements
DevOpsWorld
https://www.devopsworld.co.in/2022/04/linux-shell-script-decision-making.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/04/linux-shell-script-decision-making.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content