UNIX Unbounded: A Beginning Approach

Chapter 8: Exploring the Shell


Objectives:

This chapter returns to the operating system, discussing features of the shell. The objectives important to this chapter are:

  • using shell commands
  • using shell variables
  • more metacharacters in UNIX commands
  • UNIX utilities
  • startup files
  • understanding process handling
Concepts:

The shell is the command interface between the user and the kernel, the core of the UNIX operating system. Most UNIX commands are actually stored in external files, which are read and executed by the shell when a command is entered. In fact, the shell itself is just such a utility program, which is why it is possible to select from several available shells.

Previously, the shell we normally used in class was the Bourne shell, which is stored in the /bin directory as the sh file. Lately, the system administrator has made the C shell available to us. This shell is stored as a file called csh. Several features of shells in general are listed in the text:

  • Command Execution - it accepts commands and passes them to the system
  • Filename Substitution - shell commands can accept wild cards to execute the command across multiple files
  • I/O Redirection and Pipeline Hookup - shell commands can send programs input from sources other than the standard input device, and cause output to go to nonstandard places as well. Sending output from one program into another is piping.
  • Environment Control - customizing where files are stored, setting environment variables
  • Background Processing - commands may begin and continue while the user enters new commands
  • Interpretive Programming Language - variables and scripts may be used in the shell

The echo command is used to put messages on the screen. It can be used in a script to display messages in the same way to the user.

Some special characters need to be sent to the echo command inside quotation marks. This is because they represent commands, instead of display characters. These include:

  • \n - the escape character for a new line
  • \t - the escape character for a tab
  • \b - the escape character for a backspace
These special characters allow for more cursor control in an echo command.

The concept of quoting is important to the operating system. Your author explains three quoting methods:

  • the backslash
  • double quotes
  • single quotes
The backslash, when it precedes a special character like the star, means to interpret that character like an ordinary character. This is the opposite of the meaning used above with the escape characters. Sorry, it can't be helped. They got away with it too long ago. The backslash overrides the meaning of the dollar sign, single quotes and double quotes.

Double quotes are used to surround spaces that you want to preserve. They are also used to override all special characters except the dollar sign, single quotes and double quotes.

Single quotes are used to override the special meaning of every thing except more single quotes.

In order to explain the issue better, the following notes are reproduced from my notes for Chapter 4 of the Shell Programming class.
That author lists four characters used in quoting:

  • the single quote, or apostrophe
  • the double quote, or quotation mark
  • the back quote, or accent grave
  • the backslash character (Which is it's correct name. How odd.)

Each has its own properties and limitations. To illustrate them, we may use the grep command. This command may be used to search for a specific text string in any number of text files. (It has other uses, but this one is illustrative.)

If we want to search the file called eats for the string pizza, we could enter

	grep pizza eats

to show every line in that file containing the word pizza.

If we want to search for a multiple word string, however, we should enclose the phrase in single quotes to have the phrase processed as a single argument, such as

	grep 'single quotes' unix_file

A feature of single quotes is that dollar signs, backslashes and accents grave inside them are not interpreted as special characters. If you need to have a dollar sign used as the value-of operator, you can use double quotes instead of single quotes. The echo command will illustrate this. Suppose the command is:

	echo 'Good morning, $USER'

The message echoed would be exactly what you see here. The shell would not substitute the value of the USER variable. However,

	echo "Good morning, $USER"

would result in the user's id being substituted in the output.

The backslash means that the shell should ignore the special meaning of the next character. It is like putting single quotes around just one character. It is useful when you want to print a single or double quote to the screen, since you can use the backslash to tell the shell to treat that character like any other. This is exactly the meaning that the backslash character has in printf statements in the c language. It means to escape the normal interpretation of the next character. You can also display one type of quote by enclosing it in the other type of quote.

The back quote, or accent grave, may be used to enclose a command that you want executed, usually so that the result of that command may be used by the rest of your command line as an argument. It is difficult to see the back quote in text, and to tell it apart from the apostrophe even when you can see it. This makes it more difficult to debug scripts that use it. As an example, the command

	ls -l `which rm`

means that the shell should first run the which program on the rm file, finding out which directory the rm file is in. Then the result of that command (the path to rm) is offered to the Ls command and the result is displayed in long form.

Variables are named memory locations that hold whatever we put into them. The system keeps several variables around for its own use. These are referred to as environment variables. The user and the programmer can make their own variables. To find out what the environment variables are at any given time, enter the set command. They will be listed for you.

Variables must be named. Their names may contain letters, numbers and underscores. They must start with a letter. To create a variable named Bill, and assign a value to it, we might enter:

 Bill=1024 

Variables are case sensitive (no surprise there). When you set a value for a variable, do not use any spaces around the equal sign. You should know that all shell variables are string variables, which means that they hold a series of characters, not numeric values. The variable Bill, above, holds the characters 1, 0, 2, and 4. It does not hold a number. We will discuss how to make the shell treat it like a number later.

To access the value stored in a variable, preface the variable name with a $, as above. The shell holds series of variables that you can change, or use as they are. Some are:

  • HOME - this holds the path to your home directory
  • SHELL - holds the path to the shell command
  • TERM - holds your terminal type
  • IFS - holds the list of characters the shell will recognize as separators on a command line. DON'T mess with it.
  • PATH - holds the paths that will be searched when you type a command

The semicolon is used to separate several commands on the same command line. This is sometimes necessary to have each one carried out separately, instead of having the system consider some as arguments to others.

Parentheses may be used to group commands together. This makes the system treat them as a single command that has several steps. They should still be separated by semicolons.

You can force a process into the background by following it on the command line with a space and an ampersand. This may be most useful for processes that will take a while, when you want control of the computer again right now.

We have already discussed the pipe operator. It sends the output of one process into another process as input. It is properly used as an operator between two processes, and not to send output to a file.

The sleep command is used to tell the system to wait a specific number of seconds. It can be useful when you wish show the user a screen, wait a bit for the user to read it, and then to go on.

The ps command is used to view the status of processes. If you type it with the -a option, you will see a list of all current processes. This is useful to determine if you want to terminate one. If so, use the kill command, followed by the PID, the Process I.D. number.

The nohup command will allow you to start a process and protect it from terminating when you terminate your session. This command is not to be used without the System Administrator's approval, since it ties up resources.

As the pipe command sends output to another process, and the redirection operators send input and output as we decide, so the tee command will send output in two directions: to the default for the command, and to a specified file.

This is enough new material for one lesson. We will continue from here.