Scripting Clinic: Have a Bash with This Linux Shell - Page 2

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

Saving Output in a File
Directing command output into a file is very useful. Some operations generate a lot of output; saving it in a file lets you study it at leisure. This command creates a new file, or replaces the contents of an existing file:

$ ls -al ~ > homefiles.txt

This creates a new file, or appends to the end of an existing file:

$ ls -al ~ >> homefiles.txt

This is a fast way to append lines to an existing file:

$ cat >> homefiles.txt
testing, testing, new line

Hit CTRL + D to finish.

I like piping the output of make to a file, so I know what files were installed, and where. And to review any error messages. In this example, I'm installing JOE, Joe's Own Editor:

# make install | tee joe-makeinstall.txt

The tee command is one of those odd little specialized Unix commands that come in handy at the strangest times. It both displays the command output on the screen, and stores it in a file. Use tee -a to append, instead of overwrite.

You doubtless noticed how most of the pieces of the above examples are not Bash features at all. The only Bash bits are:

$#    prompts
>    redirection operator
|    pipe

Redirection operators work both ways, for input and output. This takes the contents of file1 and directs it to the output file, file2:

$ cat < file1 > file2

This works great when you're feeding a list, or other data in a file, to a script:

$ scriptname < filelist > outputfile.txt

A Simple Backup Script

This script demonstrates a number of common ways to use Bash. A slick Bash trick is to take a script like this, and execute it one line at a time. That's a great way to build and test a script.

To run it as a script, save it as text file, with a nice name like "backup," and make it executable:

$ chmod +x backup

There are severals ways to run it. Change to the directory it's in, and use one of these:

$ ./backup
$ bash backup

Or, do what leet geeks do, and put it in /usr/bin. Or keep it all to yourself- create your own personal scripts directory, and put it in your path:

$ mkdir ~/scripts

Append to your PATH, using your own filepath, of course.


This allows you to run the script like any other command:

$ backup

And now, the script itself:

## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments

# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution. HOME is a builtin
# Bash variable.
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"

# now we define our own variables
# location of files to backup
# name of compressed archive
# location of backup directory
# create compressed archive, copy to backup directory

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

Going through this script and understanding which parts are Bash commands, and which are Linux commands, is important for understanding how to put scripts together.

Next month in Scripting Clinic, we'll do more Bashing.

Bash home page
"Learning the Bash Shell," by Cameron Newham, Bill Rosenblatt
"Linux In A Nutshell," by Ellen Sievers

» See All Articles by Columnist Carla Schroder

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