Next Previous Contents

7. Command Shell Scripts

The time will come when you decide you want to automate some UNIX command procedures. You are in luck. UNIX shells are sophisticated programming tools.

A good plan when writing shell scripts is to write for the Bourne shell. The Bourne shell is the original UNIX shell. Anything that runs Bourne shell can be run on any other UNIX machine. The following instructions will show you how to create a Bourne shell script. The script won't do anything useful, and it won't show you nearly everything the Bourne shell language does, but it will provide a good starting point for hacking your own scripts.

To create a Bourne shell script, you must create the file, change the permissions to make it executable, then write in the UNIX commmands.

touch funfunfun

The command touch either creates a new file with zero bytes, or it sets the modify date to the present, depending on whether or not the file exists.

chmod 755 funfunfun

You must set the permissions to execute. You are allowing anybody to execute the file, but only you can write to it.

emacs funfunfun

Load the script into your favourite text editor. My favourite text editor is vi, but you don't want to run this unless you know what you are doing. Even Emacs is nasty if you are not running in X11. If you are on a modem or a VT terminal, you might want to try pico instead.

Now, we type in the actual script. You can copy and paste the following text from this window to your text editor.
#!/bin/sh
# Anything past this pound sign is a comment, except for the pound sign in 
# the command following these comments.  The string $# is the number of 
# operands you typed in on the command line.  The string $0 is the command 
# you typed.  $1 is the first operand, $2 is the second operand, etc.

OPERANDS=$#

# The `echo' command is used to print text to the screen.  Note how I have 
# left a space as the last character before the quote.  You don't actually 
# have to do this, or even use the quotes, most of the time.  Using them makes 
# the command more reliable, since some of the characters you may want to 
# use as text may be seen by the computer as commands.  Just for fun, change 
# the period at the end of `operands' to an exclamation mark, then delete the 
# trailing space -> ...operands!"  ->  Then, run the script and see what 
# happens.

echo "You provided ${OPERANDS} operands. "

if [ ${OPERANDS} = 0 ]; then
   echo "You did not provide operands, so I am going to have to list "
   echo "this directory... "
   ls -l
elif [ $1 = $LOGNAME ]; then
   # $LOGNAME is the environment variable that holds your username under most 
   # UNIXes.  Some UNIXes recognize $USER as well, or instead.
   echo "Ohh!  You typed your username! "
   echo "Hello there $1! "
elif [ -f $1 ]; then
   echo "Ooh!  You have a file called $1!  Let's list it.  "
   ls -l $1
elif [ -d $1 ]; then
   echo "Ooh!  You have a directory called $1!  Let's list its contents.  "
   ls -l $1
else
   echo "You provided operands.  I will list your directory anyway... "
   ls -l
fi
        
To run this, type
 ./funfunfun
        
To run it with an operand, type
 ./funfunfun <operand>
        
For operands, try anything, including valid file and pathames, and your username. Then, modify the program, and see what you can make it do.

The command if tests a logic command. If the command returns TRUE, it executes the following sequence of commands. If the command returns FALSE, it executes the following elif or else command, otherwise, it stops at fi, and goes to the next line in the script.

The line

elif [ -f $1 ]; then
        
tests whether or not the file specified, exists. This is done using the switch -f. This could also have been written as
elif test -f $1; then
        
There are a bunch of these switches available. You can test for valid files, for valid directories, valid pathnames, if files can be written to, and lots of other stuff. These switches are listed by the man page for test.


Next Previous Contents