Home arrow BrainDump arrow Page 3 - Executing Commands with bash

4.3 Running Several Commands in Sequence - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

BRAINDUMP ARTICLES

- 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: