Unix Shell Scripts   «Prev  Next»
Lesson 10

Unix Shell Script Conclusion

This module provided all the information you need to create and execute a shell script file, including how to designate a text file as a shell script and set the file permissions so that UNIX will execute the file’s contents.
You also learned how to start a shell script from a command line. You reviewed using the vi text editor to create a file.

What is a Script?

In the simplest case, a script is nothing more than a list of system commands stored in a file. At the very least, this saves the effort of retyping that particular sequence of commands each time it is invoked.
Example 4-10.1: cleanup: A script to clean up log files in /var/log
# Cleanup
# Run as root, of course.
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Log files cleaned up."
There is nothing unusual here, only

There is nothing unusual here, only a set of commands that could just as easily have been invoked one by one from the command-line on the console or in a terminal window. The advantages of placing the commands in a script go far beyond not having to retype them time and again. The script becomes a program -- a tool -- and it can easily be modified or customized for a particular application
Example 4-10.2: cleanup: An improved clean-up script
#!/bin/bash
# Proper header for a Bash script.
# Cleanup, version 2
# Run as root, of course.
# Insert code here to print error message and exit if not root.
LOG_DIR=/var/log
# Variables are better than hard-coded values.
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
exit # The right and proper method of "exiting" from a script.
# A bare "exit" (no parameter) returns the exit status
#+ of the preceding command.
Example 4-10.3: cleanup: An enhanced and generalized version of above scripts.
#!/bin/bash
# Cleanup, version 3
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=86 # Can't change directory?
E_NOTROOT=87 # Non-root exit error.
# Run as root, of course.
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command-line.
fi
# E_WRONGARGS=85 # Non-numerical argument (bad argument format).
#
# case "$1" in
# "" ) lines=50;;
# *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup";
# exit $E_WRONGARGS;;
# * ) lines=$1;;
# esac
#
#* Skip ahead to "Loops" chapter to decipher all this.
# -----
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory before messing with log file.

Commands and shell script components

Commands that you used in this module include:
  1. echo, to print information on the screen (or Standard Output channel)
  2. read, to collect user input from the keyboard (or Standard Input channel)
  3. chmod, to change the file permissions so that a shell script file can be executed
  4. #! to indicate the path to the shell used to run the script
  5. # to indicate a comment within a shell script
In the next module you will learn about using different types of variables within your shell scripts.