BrainDump
  Home arrow BrainDump arrow Page 5 - Executing Commands with bash
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Executing Commands with bash
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2008-06-12

    Table of Contents:
  • Executing Commands with bash
  • 4.2 Telling If a Command Succeeded or Not
  • 4.3 Running Several Commands in Sequence
  • 4.4 Running Several Commands All at Once
  • 4.5 Deciding Whether a Command Succeeds

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Executing Commands with bash - 4.5 Deciding Whether a Command Succeeds


    (Page 5 of 5 )

    Problem

    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).

    Solution

    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

    Discussion

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "bash Cookbook, Solutions and Examples for...
     

    Buy this book now. This article 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). Check it out today at your favorite bookstore. Buy this book now.

       

    BRAINDUMP ARTICLES

    - Advanced File I/O
    - More Amazing Things to Do With Pipelines
    - Pipelines Can Do Amazing Things
    - Better Command Execution with bash
    - Executing Commands with bash
    - Outsourcing: the Hoopla, the Reality
    - MySQL Plays in the Sun
    - All About SQL Functions
    - SQL: Functioning in the Real World
    - More Advanced SQL Statements
    - Beginning SQL the SEQUEL: Working with Advan...
    - Beginning SQL
    - A Look at the VI Editor
    - A Quick Tour of Boo
    - Book Review: Open Source Licensing





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT