UNIX Unbounded: A Beginning Approach

Chapter 11: Shell Programming


Objectives:

This chapter begins the discussion of shell programs that is continued in CS 211, Shell Programming. The objectives important to this chapter are:

  • writing a shell script
  • executing a shell script
  • using special characters in scripts
  • taking input from the user in a script
  • programming concept: looping
Concepts:

The concept of a script is similar to the concept of a program: a stored list of commands to be executed as the user wishes. There are some differences:

  • Programs are compiled; scripts are not
  • Programs follow their language syntax; scripts follow UNIX shell syntax
  • Programs execute quickly, because of compilation; scripts execute more slowly

Shell scripts can contain remarks, lines that are not commands, but are meant to explain command lines. Most programming languages allow for remarks. In a script, we use the pound sign as the first character on a line to make the line a remark.

To execute a script, we can take one of several approaches. First, of course, it has to exist. The script file may be created with any word processing method you choose, as long as you create a plain text file. In order to have a shell execute the commands you put in the script, you could start a new shell and pass the name of the script to it as an argument.

Another way to execute the script is to make it executable. This allows it to be executed in the current shell. This sounds dumb until you remember the three basic UNIX permissions: Read, Write and Execute. When it is created, the script may not have execute permissions for anyone, even its owner. One way to grant such permission to the owner is to enter:

  chmod u+x script_name

This would activate the execute permission to the script for the user who owns the script. Our author describes three math operators to use with the three permissions and the three types of users (does anyone else wonder if this was created by a lifeform obsessed with three's?). A plus adds a permission, a minus takes away a permission, and an equal sign sets a permission to be only what follows the equal sign. These operators are used in conjunction with the the letter u, for the user-owner, the letter g for the user-owner's group, and the letter o, for others. The permissions themselves are represented by r for read, w for write, and x for execute. (I know, it should have been an e. Not many engineers are good at English.) Now you know why I recommended using the numeric notation in a previous chapter.

Special characters may be used in scripts. One set of special characters includes the "escape" characters we saw in the last chapter. They allow us to generate a return, a backspace, a tab, and other characters on the screen. The method to use them is to pass them to the echo command inside quotes. In order to pass other, less common, characters through echo, we can use the notation "\0n", where the backslash is followed by a zero and an octal integer, which is the ASCII number of the character you want to generate. For instance, the act of echoing ASCII character 7 to the screen causes the workstation bell to ring. Well, it did twenty years ago. Now, it may cause the speaker to beep. You get the idea.

The read command in a script allows the script to ask the user for input and to read the input into a variable. This makes your shell scripts much more flexible. In the text example, a script echos a question to the screen, asking for the user's name. It is followed by the line:

  read name

which would create the variable called name, and store the user's response in it. The read command is line oriented, so it stores whatever the user types up to the enter key. Once information is stored in a variable, it may be used like any other variable to process a command or to send more information to the screen.

When storing more than one word in a variable, it is good to remember that the user does not need to enclose input in quotes, however, the programmer does need quotes.

While there are several other concepts in this chapter important to shell scripts, most are beyond the scope of this course. Let's examine some of them as a survey and discuss them in class. In particular, look at the section about loops, the mechanism to repeat a series of command a desired number of times. In order for a loop to run, a test must be passed. This means that some condition must be tested by the computer. Usually, it is based on a choice the user enters, or simply whether an action has yet taken place a specific number of times. The author's example of the for loop is illustrative. He only uses four lines:

	for count in 1 2 3
	do
	echo "In the loop for $count times."
	done

In this example, count is a variable. It is set equal to 1, the first character in the list following the word in. The word do marks the beginning of the loop to be repeated. The word done marks the end of the loop to be repeated. That only leaves the line with echo. This loop sets the variable count equal to each item found in the list following the in, one at a time. It will execute the echo line once for each value that is assigned to count.

If this is not clear, consult my notes for CS 211, which include more detailed discussions of variables and loops.