APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed


© April 2005 Tony Lawrence

There are several ways to make "if - then" decisions in shell scripts. The most direct is the "if -then" construct itself:

if [ "$x" ]
	echo x is not blank
	echo x is blank

That can also be written as

if test $x
        echo x is not blank
        echo x is blank

But watch out for quoting - if $x had never been set at all, that second method would be fine, but if it were "foo bar", the script would fail without quotes. The same warning applies when testing against a value:

if [ "$x" = "foo" ]
  	echo "x is set to foo"
  	echo "x is not set to foo"

Forgetting to quote $x will cause unexpected failures. The "test" method has many more possibilities ("man test" will show them).

Another construct you'll often see is this:

somecommand && echo "Success" || echo "Failure"

But watch out: there's a trap in there you may not see. The line above will probably always do what you think it should because 'echo "Success"'" is unlikely to fail. But if we use some different commands, it's easy to see where this could go wrong:

date && true || echo "Fail"
# prints the date 
date && : || echo "Fail"
# a better way to do that
false && date || echo "Fail"
# prints "Fail"
date && false || echo "Fail"
# prints the date and "Fail"

The && and || are useful tools, but be careful when using them together.

Another way to test is the case statement:

while :
echo "Please choose Y, N, or Q";;
read akey
case $akey in
  [Yy]) echo "Yes";break;;
  [Nn]) echo "No";;
  [qQ]) exit 0;;
  *) echo "Only Y, N or Q"
echo "Thanks";

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> shell test

1 comment

Inexpensive and informative Apple related e-books:

Take Control of IOS 11

Take Control of High Sierra

Take Control of iCloud, Fifth Edition

Take Control of Automating Your Mac

Digital Sharing Crash Course

More Articles by © Tony Lawrence

Tue Apr 26 01:47:21 2005: 373   BigDumbDinosaur

Another (more succinct) form of test is:
[ -s "/tmp/junk" ] || { 
> /tmp/junk
chmod 600 junk

This form can be used if you wish to act only upon one of the two possible outcomes of any such test (the above would zap /tmp/junk only if its length were non-zero).

Tony's admonishments about quotes and empty strings still apply.


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Whenever the literary German dives into a sentence, that is the last you are going to see of him till he emerges on the other side of his Atlantic with his verb in his mouth. (Mark Twain)

Linux posts

Troubleshooting posts

This post tagged:


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode