A normal if statement in bash looks like this

if ps ax | grep "[p]rogram" &> /dev/null; then
echo "program is running"
else
echo "program is not running"
fi

# COMMENT1: the "if statement" part is this 'ps ax | grep "[p]rogram" &> /dev/null' -- well thats what I call it in these comments, it real technical name could be different
# COMMENT2: if runs the first part of if code block when the if-statement has anexit status of success (0), and it evaluates the 'else'/2nd part in this case when the if-statement returns an exist status of fail (not 0). the if statement you can also use other programs with the if statement. I choose grep because I know that grep has an exit status of fail (not 0) when it doesnt finds something and an exit status of success (0) when it doesnt find something. also note that ps exit status doesnt matter, its only the last things exit status that matters, so in this case the grep
# COMMENT3: searching for [p]rogram instead of program because this way grep doesnt find itself - read this article for more info: http://unix.stackexchange.com/questions/74185/how-can-i-prevent-grep-from-showing-up-in-ps-results
# COMMENT4: redirecting the output to garbage so that we dont see the output of ps. we just want it to print "program is running" or "program is not running", we dont want to see the ps. this is good practice for if statement, the actual if part, rarely does anyone want to see that.
# COMMENT5: I redirected with "&> /dev/null", however I could of used just ">  /dev/null", but then if there was errors it would cause issues. Also note that "&>" is the same as "> /dev/null 2>&1" or "2> /dev/null > /dev/null". if your using "> /dev/null 2>&1"  then make sure to do ">" first so that "2>" knows where "&1" is, meaning dont do " 2>&1 > /dev/null".

Here is how to make it one line

if ps ax | grep "[p]rogram" &> /dev/null; then echo "program is running"; else echo "program is not running"; fi;

# COMMENT1: notice there is no ; after the 'then' nor the 'else'. 
# COMMENT2: the ; after the fi is optional
# COMMENT3: make sure there is a space after the "if" followed by the if-statement
# COMMENT3: make sure there is a semicolon space "; " right after the if-statement. Notice how after the "ps ax | grep "[p]rogram" &> /dev/null" there is a "; ". making it "ps ax | grep "[p]rogram" &> /dev/null; "
# COMMENT4: format:  if if-statement; then success-part; else fail-part; fi;
# if-statement should be a set of commands (that can be tied with pipes) where the last command has a meaningful exit status (0 exit status which means success will run the success-part code, and anything other than 0 will run fail-part code)

And here is how to make it one line with ampersands ands and pipe ors

(ps ax | grep "program" &> /dev/null) && echo "success" || echo "fail"
(ps ax | grep "program" &> /dev/null) && VAR1="success" || VAR1="fail"

# COMMENT1: the format is (if-statement) && success-part || fail-part
# COMMENT2: notice that the success-part is firt and the fail-part is second, do not put the fail part first or else the behaviour is bad.. for example:
# (ps ax | grep "program" &> /dev/null) || VAR1="fail" && VAR1="success" 
# this will output "success" if it succedds as it should, but if it fails it will output both "fail" followed by "success"

Finally. I think its important to take back the format of these if statements

# IF CODE BLOCK FORMAT
if if-statement; then
success-part;
else
fail-part;
fi;

# IF ONE LINER
if if-statement; then success-part; else fail-part; fi;

# OTHER IF ONE LINER
(if-statement) && success-part || fail-part;

# NOTE: that very last ; is optional

Also you will probably note that its important to know the exit status of your various programs. You will find that most programs have exit statuses that are logical. For example grep returns not 0 when it doesnt find something, and 0 for success when it does find something. Also the most common operation with if is the test program which has very logical exit statuses. Test can be run with the command “test test-statement” or “[ test-statement ]”. More reading here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

# IF CODE BLOCK FORMAT WITH TEST
if [ test-statement ]; then
success-part;
else
fail-part;
fi;

# IF ONE LINER WTIH TEST
if [ test-statement ]; then success-part; else fail-part; fi;

# OTHER IF ONE LINER WITH TEST
[ test-statement ] && success-part || fail-part;

# NOTE: that very last ; is optional

 

Leave a Reply

Your email address will not be published. Required fields are marked *