Home arrow BrainDump arrow Page 5 - Better Command Execution with bash

4.10 Running All Scripts in a Directory - BrainDump

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.

TABLE OF CONTENTS:
  1. Better Command Execution with bash
  2. 4.7 Running Long Jobs Unattended
  3. 4.8 Displaying Error Messages When Failures Occur
  4. 4.9 Running Commands from a Variable
  5. 4.10 Running All Scripts in a Directory
By: O'Reilly Media
Rating: starstarstarstarstar / 5
June 19, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Problem

You want to run a series of scripts, but the list keeps changing; youíre always adding new scripts, but you donít want to continuously modify a master list.

Solution

Put the scripts you want to run in a directory, and let bash run everything that it finds. Instead of keeping a master list, simply look at the contents of that directory. Hereís a script that will run everything it finds in a directory:

Put the scripts you want to run in a directory, and let run everything that it finds. Instead of keeping a master list, simply look at the contents of that directory. Hereís a script that will run everything it finds in a directory:

  for SCRIPT in /path/to/scripts/dir/*
  do
     
if [ -f $SCRIPT -a -x $SCRIPT ]
     
then
         
$SCRIPT
      fi
  done

Discussion

We will discuss the for loop and the if statement in greater detail in Chapter 6, but this gives you a taste. The variable $SCRIPTwill take on successive values for each file that matches the wildcard pattern*, which matches everything in the current directory (except invisible dot files, which begin with a period). If it is a file (the-ftest) and has execute permissions set (the-xtest), the shell will then try to run that script.

In this simple example, we have provided no way to specify any arguments to the scripts as they are executed. This simple script may work well for your personal needs, but wouldnít be considered robust; some might consider it downright dangerous. But we hope it gives you an idea of what lies ahead: some programming-language-style scripting capabilities.

See Also

ē Chapter 6 for more about for loops and if statements



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