| By Dave Taylor | Article Rating: |
|
| December 13, 2004 12:00 AM EST | Reads: |
8,557 |
In the October issue of LWM (Vol. 2, issue 10) I explored the basic logic of conditional execution sequences on the command line, differentiating between "&&" and "||". If you didn't read it, I encourage you to check it out at http://linux.intuitive.com/, along with all the other back columns of Wicked Cool Linux.
In addition, I briefly explored how you can use parentheses to group a sequence of commands into a logical block and then have that block be part of a conditional execution sequence:
(grep dialup /var/log/modem | \
mail -s dialup taylor) || echo failed
This time I want to delve into one of the cornerstones of shell scripting, a command that not only often hides as a punctuation character, but is rarely utilized to its full capabilities: test.
The Basics of the Test Command
The best place to start learning about the test command is to check out the man page associated with it. You'll be amazed at just how many different conditional tests it supports. Try man test on your system to see what I mean."But I've read lots of shell scripts and have never seen 'test' referenced?" you may be thinking. That's because test usually masquerades as "[":
$ ls -l /bin/{test,[}
-r-xr-xr-x 1 root wheel 18288 14 Jun 01:16 /bin/[
-r-xr-xr-x 1 root wheel 18288 14 Jun 01:16 /bin/test
(Notice the useful Bash grouping notation there too, with x/{a,b} expanding to x/a and x/b. A helpful shortcut!)
The test command - in either guise - is used most commonly with a conditional if statement, which takes the form:
if condition then command block f i
Typically, scripters insert a semicolon between the condition and the "then" so that it's three lines, not four. Here's a very common simple condition, utilizing the -e test that returns true (zero) if the specified file exists and false (non-zero) if it doesn't:
if [ -e "myfile" ] ; then echo myfile exists f i
Notice that if you use the "[" shortcut to test, you are required to include the closing "]", which is good form anyway. However, if you wanted to call test directly, the following is 100% identical in its result:
if test -e "myfile" ; then
Notice the use of spaces in the if statements above. Whether you use "[" or "test", you'll find that omitting needed spaces around the elements of the conditional will cause scary and confusing error messages, so don't try to be too succinct in your scripts.
Understanding Return Codes
Before going any further, it's worth a brief sidetrack to explain how Unix commands communicate success or failure: through a numeric return code that is defined to be zero on success, non-zero and a meaningful error code value on failure. You can see this when you look at the test man page and it shows:
RETURN VALUES
The test utility exits with one of the following values:
0 expression evaluated to true.
1 expression evaluated to false or expression was missing.
>1 An error occurred.
This return code is exactly what the shell uses when it evaluates conditionals within an if statement, hence the existence of the two oddly named true and false commands: they return 0 or non-zero, respectively, and are used in conditional statements. The following is not only legal but it'll work just fine in Unix:
if true ; then echo true is true, yowza else echo how odd. true is false f i
(This time notice that I slipped in an else clause.)
Let's go back to the test statement, though, now that you can see how the shell implements conditional statement blocks.
Tests on Files
There are lots of different tests that can be performed with the test command, so let's start by just looking at the most common file-related conditions in Table 1.With that knowledge, and the addition that "!" negates (flips) the logic of any given conditional expression, the following snippet of script should make sense to you:
if [ ! -e $file ] ; then echo "$file doesn't exist" f i if [ -d $file ] ; then echo "$file is a directory" f i if [ -f $file ] ; then echo "$file is a file" f i
There's oh so much more you can do with conditionals, especially as we continue to explore the many looping and flow-control mechanisms in the shell, but let's stop here and wait until next month to delve into this any further.
Published December 13, 2004 Reads 8,557
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Dave Taylor
Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Linux Virtualization and Tired Open Source Myths
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- Amazon Kindle Fire Gets Its Own 'Personal Cloud Desktop' with AlwaysOnPC App Launch
- SPIRIT DSP Receives 2011 INTERNET TELEPHONY Product of the Year Award
- Hadoop Quickstart: Use Whirr to automate standup of your distributed cluster on Rackspace
- Jury Gets Novell Antitrust Case Against Microsoft
- The Utility Infrastructure Security Market 2012-2022: Cybersecurity & Smart Grids
- FORTUNE Magazine Names Rackspace Among “100 Best Companies to Work For”
- iFollowOffice Turns to Virtual Bridges and Savvis for On-Demand Virtual Desktop Services
- EnterpriseDB Announces Availability of Postgres Plus Cloud Database
- i-Technology in 2012: Five Industry Predictions
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Amazon to Rent Out Supercomputers
- Amazon Émigré Starts Network Monitoring Firm
- HP’s Putting a Back Door in the Itanium Alamo
- Linux Virtualization and Tired Open Source Myths
- CloudLinux Announces Preferred Partner Program
- MapR Pushes the Hadoop Envelope
- Rightware Announces Gaming Performance Benchmark for OpenGL ES 3.0/Halti
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- 3Dconnexion Announces its Newest 3D Mouse - the SpaceMouse Pro
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- A Closer Look at Damn Small Linux
- Linus' Top Ten SCO Barbs
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .

















