Scripting Clinic: The Bash Continues - Page 2

By Carla Schroder | Posted Mar 16, 2004
Page 2 of 2   |  Back to Page 1
Print ArticleEmail Article
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
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

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

if
    command was successful
then
    go to the next step
else
    make corrections
fi
    all done

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

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

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:

if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi

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
/home/carla

Putting It All Together

#!/bin/bash
# a simple script for converting wav files
# to ogg format, and moving the ogg files
# into a separate directory
# first, our directory variables
OGGDIR=~/TMP/oggfiles
WAVDIR=~/1wavtest

#change to directory the wavs are in
cd $WAVDIR

# 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;
done

#check to see if the ogg directory exists
if [ -e $OGGDIR ]
then
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
else
echo "The ogg directory does not exist, so I will create it."
mkdir -p $OGGDIR
mv *.ogg $OGGDIR
echo "All finished, enjoy your music."
fi

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.

Resources
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

Comment and Contribute
(Maximum characters: 1200). You have
characters left.
Get the Latest Scoop with Enterprise Networking Planet Newsletter