Conditions
Linux Commands - Shells

Conditionals:
Conditions are elements that you will use often. With conditions you will test for an element and then create a response to the results of that test.  In most situations, conditions will be a very important part of your scripts.  

In the example below, the if starts the condition and then inside the brackets, which equates to the test command, is the element that is tested, is the number 15 equal to (-eq) the number 15, of course it is true and will exit with a “0” to indicate it is true.  So the then will evaluate the zero and echo the Success line.  If it exited with any number besides “0”, it would indicate that it failed so the second Failure line would echo.

Lesson 13

#!/bin/sh 
# Conditions 
if [ 15 -eq 15 ] 
then 
          echo Success! They are equal. 
else 
         echo Failure! They are not equal. 
fi 


Conditions in bASH


This example script uses several features to gather information and then send it to an administrator.    The variable “ADMIN” is who the information will get emailed to.    The script checks for a text string in the rc.local file that would indicate that a firewall script is installed. Note the output of the grep command is sent to /dev/null which will prevent it from showing up on the screen.  If the test is successful it will return a “0”, any other output indicates failure.  So the if statement is checking for anything but “0” and if that is found then a message and an email is sent to the administrator.  If the output is indicated by a “0” for success then a message is sent to suggest the firewall script is in place.


#!/bin/bash 
 # check for firewall script 
 
ADMIN=

 This e-mail address is being protected from spambots. You need JavaScript enabled to view it

  
 grep "sh /etc/rc.d/rc.firewall" /etc/rc.d/rc.local > /dev/null 
 if [ "$?" -ne "0" ] 
then 
               echo "No Firewall Script" | mail -s "No Firewall" $ADMIN; echo No Firewall 
else 
               echo "Firewall in Place" | mail -s "Firewall in Place" $ADMIN; echo Firewall 
fi 
exit 0 




This script is checking for  the correct permissions on the authorized_keys file in an user's .ssh directory.  The test starts by setting an email for who will receive the output.  Then whoever executes the script, the script moves into the user's hidden .ssh directory and use the if and a test to check to see if it has executable permissions, which will cause it to fail.  The “-x” is what checks for the executable bit.  $file is the variable established in the line above.

This script introduces a second test, the “elif” will test for two conditions, to see if the script is readable and writeable, both of which are required for it to work correctly.  If that is true then message is sent to the administrator.

#!/bin/bash 
# Test for authorized_keys permissions 
ADMIN=

 This e-mail address is being protected from spambots. You need JavaScript enabled to view it

 
cd ~ 
cd .ssh 
file=authorized_keys 
if [ -x $file ] 
then 
              echo "Failure" | mail -s "Wrong Permissions" $ADMIN; echo Failure 
elif [[ -r $file && -w $file ]] 
then 
             echo "Success" | mail -s "Correct Permissions" $ADMIN;echo Success 
fi 
exit 0 


Here is a script that will check to see if quotas exist on a partition.  The script starts by creating an ADMIN variable so that someone can get a message about the results of the test.  The QUOTA is a variable that uses command substitution to check the /etc/fastab and specifically the /home partition to see it is has information about quotas.  The command cut is used to gather the information in the fourth field (-f4), noting that the delimiter is empty space (“”).  The  QUOTA variable is then examined by a second command to look for a text string “usrquota” which would indicate a quota is set for users on this partition.  The egrep command will return either a “0” to indicate that the text string exists or it will return some other number to indicate failure.  Any return except for “0” (-ne 0) will lead to  a message sent that quotas do not exist.  If it returns a “0” it will send the opposite message.


#!/bin/bash 
# check for quotas 
ADMIN=

 This e-mail address is being protected from spambots. You need JavaScript enabled to view it

  
QUOTA=$(grep ^"LABEL=/home" /etc/fstab | cut -d "" -f4) 
echo $QUOTA | egrep "usrquota" 
if [ "$?" -ne "0" ] 
then 
              echo "No Quotas" | mail -s "No Quotas" $ADMIN; echo No Quotas 
else 
             echo "Quotas Are Used" | mail -s "Quotas Installed" $ADMIN; echo Quotas 
fi 
exit 0 


This if then script will show you how you could cycle through  a list to locate conditional elements as it goes through the list.  The for loop sets up a variable states and then works its way through a list of specific states.  An if then statement allows it to select one state and then echo a message for that state and a separate message for other states as it cycles through.

#!/bin/bash 
# conditions for states 
for states in montana idaho washington wyoming 
do 
if [ "$states" = "montana" ] 
then 
            echo "$states The Last Great Place" 
else 
             echo "$states Sorry nothing else compares" 
fi 
done