Sunday, September 07, 2008

Shell script if statement

Shell script using if statement to check if process is running:
================================================================

#!/bin/bash

sendmail=`(ps -ef |grep -v grep |grep sendmail)`
if [ -z "$sendmail" ];

then
echo "Sendmail is not running"

else
echo "Sendmail is running"

fi


WHat each line means:
---------------------
1) #!/bin/bash
This generates a Process ID for shell

2) PROCESS=`(ps -ef |grep -v grep |grep sendmail)`
Defining Variable Process:
# When we grep a process ps -ef |grep sendmail, the output result also shows "grep sendmail" line, in order to avoid that we use grep -v grep.


3) if [ -z "$sendmail" ];

-z option tells it should return a null value(no value)

Check for more : man bash

-n str True if string str is not a null string
-z str True if string str is a null string

No comments: