BrainDump
  Home arrow BrainDump arrow Page 2 - Executing Commands 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? 
Google.com  
BRAINDUMP

Executing Commands with bash
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    Executing Commands with bash - 4.2 Telling If a Command Succeeded or Not
    ( Page 2 of 5 )

    Problem

    You need to know whether the command you ran succeeded.

    Solution

    The shell variable $? will be set with a non-zero value if the command fails—provided that the programmer who wrote that command or shell script followed the established convention:

      $ somecomman d
      it works...
      $ echo $?
      0
      $ badcommand
      it fails...
      $ echo $?
      1
      $

    Discussion

    The exit status of a command is kept in the shell variable referenced with $?. Its value can range from 0 to 255. When you write a shell script, it’s a good idea to have your script exit with a non-zero value if you encounter an error condition. (Just keep it below 255, or the numbers will wrap around.) You return an exit status with the exit statement (e.g., exit 1 or exit 0 ). But be aware that you only get one shot at reading the exit status:

      $ badcommand
      it fails...
      $ echo $?
      1
      $ echo $?
      0
      $

    Why does the second time give us 0 as a result? Because the second time is reporting on the status of the immediately preceding echo command. The first time we typed echo $? it returned a 1 , which was the return value of badcommand. But the echo command itself succeeds, therefore the new, most-recent status is success (i.e., a 0 value). So you only get one chance to check it. Therefore, many shell scripts will immediately assign the status to another shell variable, as in:

      $ badcommand
      it fails...
      $ STAT=$?
      $ echo $STAT
      1
      $ echo $STAT
      1
      $

    We can keep the value around in the variable $STAT and check its value later on.

    Although we’re showing this in command-line examples, the real use of variables like $? comes in writing scripts. You can usually see if a command worked or not if you are watching it run on your screen. But in a script, the commands may be running unattended.

    One of the great features of bash is that the scripting language is identical to com mands as you type them at a prompt in a terminal window. This makes it much easier to check out syntax and logic as you write your scripts.

    The exit status is more often used in scripts, and often in if statements, to take different actions depending on the success or failure of a command. Here’s a simple example for now, but we will revisit this topic in future recipes:

      $ somecomman d
      ...
      $ if (( $? )) ; then echo failed ; else echo OK; fi

    See Also
    1. Recipe 4.5, "Deciding Whether a Command Succeeds"
    2. Recipe 4.8, "Displaying Error Messages When Failures Occur"



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

       

    BRAINDUMP ARTICLES

    - Migrating Oracle to PostgreSQL with Enterpri...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek