Scripting Clinic: The Bash Continues - Page 2

 By Carla Schroder
Page 2 of 2   |  Back to Page 1
Print Article
Continued From Page 1

Cleaning Up
The above command dumps everything in the working directory, so your new files are jumbled up with your old files. Why not do a little housecleaning:

$ mkdir ~/oggfiles
$ mv *.ogg ~/oggfiles

Conditional statements are invaluable in scripting. This is the most common style of usage:

    command was successful
    go to the next step
    make corrections
    all done

Here's an example from last month's Scripting Clinic:

if [ -e $HOME/1backups ]
echo "The backup directory exists."
echo "A backup directory does not exist, and will be created."
mkdir $HOME/1backups
echo "A backup directory has been created"

This has all the elements of a good, useful conditional statement. It tests for the existence of a backup directory, then creates one if the directory does not exist. It prints a running commentary to the screen, so you know what's going on. It even has a bit of error-checking, and informs you if the backup succeeded, or failed:

echo "Jolly good show, the backup worked!"
echo "Dagnabit, the backup failed. Time to debug."

It is better to not fail or succeed silently, always echo some kind of confirmation.

A common source of confusion is the $, or variable substitution. $ means "substitute the value of the following variable." So mkdir $HOME/1backups does not create a directory named HOME/1backups; it substitutes the home directory of the user running the script. HOME is a builtin Bash variable. You can test this yourself:

$ echo $HOME

Putting It All Together

# a simple script for converting wav files
# to ogg format, and moving the ogg files
# into a separate directory
# first, our directory variables

#change to directory the wavs are in

# use sox for the file conversion
# all wavs in the directory are selected
for i in *.wav ;
do echo $i ;
sox $i ${i%%.wav}.ogg ;
echo ${i%%.wav}.ogg;

#check to see if the ogg directory exists
if [ -e $OGGDIR ]
echo "The ogg directory exists, and the ogg files will be moved into it."
mv *.ogg $OGGDIR

# create it if it's not already present
echo "The ogg directory does not exist, so I will create it."
mkdir -p $OGGDIR
mv *.ogg $OGGDIR
echo "All finished, enjoy your music."

Pay no heed to mean people who criticize your scripts, and call them names like "kludgy" and "inelegant." There are many ways to do even the simplest tasks in Linux. If it works, it is good. However, it's often helpful to share your scripts on mailing lists, and put other people to work refining and improving them. Let 'em be snarky, as long as they are helpful.

Don't miss next month's Scripting Clinic, where we shall introduce ourselves the powerful, yet understandable, Python.

man bash
Linux In A Nutshell, by Ellen Siever
Scripting Clinic: Have a Bash with This Linux Shell
BASH Programming - Introduction HOW-TO

» See All Articles by CrossNodes contributor Carla Schroder

This article was originally published on Mar 16, 2004
Get the Latest Scoop with Networking Update Newsletter