BrainDump
  Home arrow BrainDump arrow Better Command Execution with bash
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Better Command Execution with bash
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-06-19


    Table of Contents:
  • Better Command Execution with bash
  • 4.7 Running Long Jobs Unattended
  • 4.8 Displaying Error Messages When Failures Occur
  • 4.9 Running Commands from a Variable
  • 4.10 Running All Scripts in a Directory

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Better Command Execution with bash
    ( Page 1 of 5 )

    In this second part of a two-part series on executing commands with the bash shell, you will learn how to use fewer if statements, display error messages when failures occur, and more. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    4.6 Using Fewer if Statements

    Problem

    As a conscientious programmer, you took to heart what we described in the previous recipe, Recipe 4.5. "Deciding Whether a Command Succeeds." You applied the concept to your latest shell script, and now you find that the shell script is unreadable, if with all those if statements checking the return code of every command. Isn’t there an alternative?

    Solution

    Use the double-ampersand operator in bash to provide conditional execution:

      $ cd mytmp && rm *

    Discussion

    Two commands separated by the double ampersands tells bash to run the first command and then to run the second command only if the first command succeeds (i.e., its exit status is 0). This is very much like using an if statement to check the exit status of the first command in order to protect the running of the second command:

      cd mytm p
      if (( $? )); then rm * ; fi

    The double ampersand syntax is meant to be reminiscent of the logical and operator in C Language. If you know your logic (and your C) then you’ll recall that if you are evaluating the logical expression A AND B , then the entire expression can only be true if both (sub)expression A and (sub)expression B evaluate to true. If either one is false, the whole expression is false. C Language makes use of this fact, and when you code an expression like if (A && B) { ... } , it will evaluate expression A first. If it is false, it won’t even bother to evaluate B since the overall outcome (false) has already been determined (by A being false).

    So what does this have to do with bash? Well, if the exit status of the first command (the one to the left of the && ) is non-zero (i.e., failed) then it won’t bother to evaluate the second expression—i.e., it won’t run the other command at all.

    If you want to be thorough about your error checking, but don’t want if statements all over the place, you can have bash exit any time it encounters a failure (i.e., a non- zero exit status) from every command in your script (except in while loops and if statements where it is already capturing and using the exit status) by setting the -e flag.

      set - e
      cd mytmp
      rm *

    Setting the -e flag will cause the shell to exit when a command fails. If the cd fails, the script will exit and never even try to execute the rm * command. We don’t recommend doing this on an interactive shell, because when the shell exits it will make your shell window go away.

    See Also

    • Recipe 4.8, "Displaying Error Messages When Failures Occur" for an explanation of the || syntax, which is similar in some ways, but also quite different from the && construct



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

       

    BRAINDUMP ARTICLES

    - Demystifying SELinux on Kernel 2.6
    - Yahoo and Microsoft Create Ad Partnership
    - The Advantages of Obscure Open Source Browse...
    - Dell Announces CSI-style Digital Forensics S...
    - Milepost GCC Speeds Open-Source Development
    - Learn These 10 Programming Languages
    - Tomcat Capacity Planning
    - Internal and External Performance Tuning wit...
    - Tomcat Benchmark Procedure
    - Benchmarking Tomcat Performance
    - Tomcat Performance Tuning
    - Wubi: Windows-based Ubuntu Installer
    - Configuring and Optimizing Your I/O Scheduler
    - Linux I/O Schedulers
    - Advising the Linux Kernel on File I/O





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT