-->
Previous Table of Contents Next


Writing Programs with the Shell

To write programs that use the shells, you must know about variables and control structures. Don’t let either term scare you. A variable is an object that, at any one time, has one of possibly many different values assigned to it. Control structures specify the way you can control the flow of execution of a script. There are two basic types of control structures: decision structures (such as if…then…else structures or case structures), and iterative structures or loops (such as a for or while loop). With a decision structure, you choose a course of action from one or more alternatives, usually depending on the value of a variable or the outcome of a command. With an iterative structure, you repeat a sequence of commands. The earlier section “Setting the Shell Environment” discusses shell variables; the later section “Programming with Control Structures” provides more information on control structures.

Using echo

You can use the echo command to display informative messages about what’s happening in a shell script. The echo command displays its arguments—that is, whatever follows the word echo—on-screen. Putting a string of characters in quotation marks ensures that all the characters are displayed. You also can redirect the results of echo to a file.

This command:


echo “Please stand by …”

displays the following line on the terminal screen:


Please stand by …

The following command puts Please stand by … in the file named messg:


echo “Please stand by …” > messg


TIP:  Using the echo command can make users feel as though something is happening when they enter a command—a particularly good idea if the command doesn’t give any output for several seconds or longer.

The echo command is also useful when you want to trace a shell script. Using the echo command at key points tells you what’s happening in a script. Here is the file whatsup with an echo command or two added:


echo “ Let’s see who is on the system.”

who

echo “ Any appointments? ”

calendar

date

echo “ All done”

When you run the whatsup file, you see the following:


$ whatsup

  Let’s see who is on the system.

sarah     tty01     Dec 20 08:51

brad      tty03     Dec 20 08:12

ernie     tty07     Dec 20 08:45

  Any appointments?

12/20     Sales meeting at 1:45

12/21     party after work!

Mon Dec 20 09:02 EST 1993

  All done

$

Using Comments

It’s always possible that after you write a shell script and don’t use it for a while, you’ll forget what the shell script does or how it accomplishes its task. Put comments in your shell scripts to explain the purpose of the task and how the task is achieved. A comment is a note to yourself or whoever is reading the script. The shell ignores comments; they’re important to and for human beings.

The pound sign (#) signals the beginning of a comment to the shell. Every character from the pound sign to the end of the line is part of that comment. Here’s how you might comment the shell script whatsup:


# Name:      whatsup

# Written:   1/19/97, Patty Stygian

# Purpose:   Display who’s logged in, appointments, date

      echo “Let’s see who is on the system.”

     who          # See who is logged in

     echo “ Any appointments? ”

     calendar     # Check appointments

     date         # Display date

     echo “ All done”

Run the shell script again, and you see the same results as before. The comments don’t change the behavior of the shell script in any way.

Using Variables in Shell Programs

To use variables, you must know how to give a variable a value and how to access the value stored in a variable. Using the value of a variable is straightforward, but there are four ways of giving a variable a value:

  Using direct assignment
  Using the read command
  Using command-line parameters
  Substituting the output of a command

Using Direct Assignments

The most direct way to give a variable a value is to write an expression such as this:


myemail=edsgar@crty.com

This expression gives the variable myemail the value edsgar@crty.com. Don’t include spaces on either side of the equal sign (=). The direct-assignment method of assigning a value to a variable takes the following form:


variable-name=variable-value

If variable-value contains blanks, enclose the value in quotation marks. To assign an office address of Room 21, Suite C to the variable myoffice, for example, use the following command:


myoffice=“Room 21, Suite C”

The shell retrieves the value of the variable whenever it sees a dollar sign ($) followed by the name of a variable. You can see that when the following two statements are executed:


echo “ My e-mail address is $myemail”

echo “ My office is $myoffice”

Suppose that you frequently copy files to a directory named /corporate/info/public/sales. To copy a file named current to that directory, enter this command:


cp current /corporate/info/public/sales

To make this easier, you can assign the long directory name to the variable corpsales with the following expression:


corpsales=/corporate/info/public/sales

Now, to copy the current file to that directory, you enter the following:


cp current $corpsales

The shell replaces $corpsales with the value of the variable corpsales and then issues the copy command.

Using the read Command

The read command takes the next line of input and assigns it to a variable. The following shell script extends the preceding corpsales example to ask the user to specify the name of the file to be copied:


# Name: copycorp

# Purpose: copy specified file to

#            /corporate/info/public/sales

         corpsales=/corporate/infor/public/sales

     echo “Enter name of file to copy”     # prompt user

     read filename                          # get file name

     cp $filename $corpsales               # do the copy

The read command pauses the script and waits for input from the keyboard. When <Return> is pressed, the script continues. If <Ctrl-d> (sometimes represented as ^D) is pressed while the read command is waiting for input, the script is terminated.


Previous Table of Contents Next