Home arrow BrainDump arrow Page 5 - Executing Commands with bash

4.5 Deciding Whether a Command Succeeds - BrainDump

This two-part article will explain how to launch programs in the bash shell, used with Linux and Unix operating systems. It is excerpted from chapter four of the bash Cookbook, Solutions and Examples for bash Users, written by Carl Albing, JP Vossen and Cameron Newham (O'Reilly, 2007; ISBN: 0596526784). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Executing Commands with bash
  2. 4.2 Telling If a Command Succeeded or Not
  3. 4.3 Running Several Commands in Sequence
  4. 4.4 Running Several Commands All at Once
  5. 4.5 Deciding Whether a Command Succeeds
By: O'Reilly Media
Rating: starstarstarstarstar / 2
June 12, 2008

print this article




You need to run some commands, but you only want to run certain commands if certain other ones succeed. For example, youíd like to change directories (using the cd command) into a temporary directory and remove all the files. However, you donít want to remove any files if the cd fails (e.g., if permissions donít allow you into the directory, or if you spell the directory name wrong).


We can use the exit status ($?) of the cd command in combination with an ifstatement to do the rm only if the cd was successful.

  cd mytmp
  if (( $? )); then rm * ; fi


Obviously, you wouldnít need to do this if you were typing the commands by hand. You would see any error messages from the cd command, and thus you wouldnít type the rm command. But scripting is another matter, and this test is very well worth doing to make sure that you donít accidentally erase all the files in the directory where you are running.

Letís say you ran that script from the wrong directory, one that didnít have a subdirectory named mytmp. When it runs, the cd would fail, so the current directory remains unchanged. Without theifcheck (the cd having failed) the script would just continue on to the next statement. Running therm *would remove all the files in your current directory. Ouch. Theifis worth it.

So how does$?get its value? It is the exit code of the command. For C Language programmers, youíll recognize this as the value of the argument supplied to theexit()function; e.g.,exit(4);would return a 4. For the shell, zero is considered success and a non-zero value means failure.

If youíre writing bash scripts, youíll want to be sure that your bash scripts explicitly set return values, so that$?is set properly from your script. If you donít, the value set will be the value of the last command run, which you may not want as your result.

See Also
  • Recipe 4.2, "Telling If a Command Succeeded or Not"
  • Recipe 4.6, "Using Fewer if Statements"

Please check back next week for the conclusion to this article.

>>> More BrainDump Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates


Dev Shed Tutorial Topics: