BrainDump
  Home arrow BrainDump arrow Page 3 - 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.3 Running Several Commands in Sequence


    (Page 3 of 5 )

    Problem

    You need to run several commands, but some take a while and you don’t want to wait for the last one to finish before issuing the next command.

    Solution

    There are three solutions to this problem, although the first is rather trivial: just keep typing. A Linux or Unix system is advanced enough to be able to let you type while it works on your previous commands, so you can simply keep typing one command after another.

    Another rather simple solution is to type those commands into a file and then tell bash to execute the commands in the file—i.e., a simple shell script.

    Assume that we want to run three commands: long, medium, and short, each of whose execution time is reflected in its name. We need to run them in that order, but don’t want to wait around for long to finish before starting the other commands. We could use a shell script (aka batch file). Here’s a primitive way to do that:

      $ cat > simple.script
      long
      medium
     
    short
      ^D               # Ctrl-D, not visible
      $ bash ./simple.script

    The third, and arguably best, solution is to run each command in sequence. If you want to run each program, regardless if the preceding ones fail, separate them with semicolons:

      $ long ; medium ; short

    If you only want to run the next program if the preceding program worked, and all the programs correctly set exit codes, separate them with double-ampersands:

      $ long && medium && short

    Discussion

    The cat example was just a very primitive way to enter text into a file. We redirect the output from the command into the file named simple.script (for more on redirecting output, see Chapter 3). Better you should use a real editor, but such things are harder to show in examples like this. From now on, when we want to show a script, we’ll just either show the text as disembodied text not on a command line, or we will start the example with a command like cat filename to dump the contents of the file to the screen (rather than redirecting output from our typing into the file), and thus display it in the example.

    The main point of this simple solution is to demonstrate that more than one command can be put on the bash command line. In the first case the second command isn’t run until the first command exits, and the third doesn’t execute until the second exits and so on, for as many commands as you have on the line. In the second case the second command isn’t run unless the first command succeeds, and the third doesn’t execute until the second succeeds and so on, for as many commands as you have on the line.

    More BrainDump Articles
    More By O'Reilly Media


       · 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 6 hosted by Hostway
    Stay green...Green IT