Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Interactive Versus Noninteractive Shells

When the shell displays a prompt for you, it is running in interactive mode.


Interactive mode means that the shell expects to read input from you and execute the commands that you specify. This mode is called interactive because the shell is interacting with a user. This is usually the mode of the shell that most users are familiar with: you log in, execute some commands, and log out. When you log out using the exit command, the shell exits.


The shell can be run in another mode, called noninteractive mode. In this mode, the shell does not interact with you; instead it reads commands stored in a file and executes them. When it reaches the end of the file, the shell exits.

How login Starts a Shell

When the login program starts a shell, it basically executes the following command:

/bin/sh

By issuing this command, it puts the shell into interactive mode. You can start a shell in interactive mode by issuing the same command at the prompt:

$ /bin/sh
$

The first prompt $ is displayed by the shell that login started; the second one is displayed by the shell you started. To exit from this shell, use the exit command:

$ exit
$

The prompt that is displayed now is from the original shell started by login. Typing exit at this prompt logs you out.

How to Start the Shell Noninteractively

You can start the shell noninteractively as follows:

$ /bin/sh filename

Here filename is the name of a file that contains commands to execute. As an example, consider the compound command:

$ date ; who

Put these commands into a file called logins. First open a file called logins in an editor and type the command shown previously. Assuming that the file is located in the current directory, after the file is saved, the command can run as

$ /bin/sh logins

This executes the compound command and displays its output.


This is the first example of a shell script. Basically, a shell script is a list of commands stored in a file that the shell executes noninteractively.

Initialization File Contents

Usually the shell initialization files are quite short. They are designed to provide a complete working environment with as little overhead as possible for both interactive and noninteractive shells.

The file /etc/profile is maintained by the system administrator of your UNIX machine and contains shell initialization information required by all users on a system.

The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes

  The type of terminal you are using
  A list of directories in which to locate commands
  A list of directories in which to locate manual pages for commands

Setting the Terminal Type

Usually the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the autoconfiguration process guesses your terminal incorrectly. This can occur when you are using a dial-up or modem connection.

If your terminal is set incorrectly, the output of commands might look strange, or you might not be able to interact with the shell properly. To make sure that this is not the case, most users set their terminal to the lowest common denominator as follows:

TERM=vt100

When I introduce the case statement in Chapter 10, “Flow Control,” you will see a more advanced method of setting the terminal type that enables access to advanced terminal features.

Setting the PATH

When you type the command

$ date

the shell has to locate the command date before it can be executed. The PATH specifies the locations in which the shell should look for commands. Usually it is set as follows:

PATH=/bin:/usr/bin

Each of the individual entries separated by the colon character, :, are directories. Directories are discussed in Chapter 4.

If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, a message similar to the following appears:

$ hello
hello: not found

Setting the MANPATH

In UNIX, online help has been available since the beginning. In the section “Getting Help” I will discuss how to access it using the man command.

In order for you to access all the available help, you have to tell the shell where to look for the online help pages. This information is specified using the MANPATH. A common setting is

MANPATH=/usr/man:/usr/share/man

Like the path, each of the individual entries separated by the colon character, :, are directories.

When you use the man command to request online help as follows, the man command searches every directory given in the MANPATH for an online help page corresponding to the topic you requested.

$ man who

In this case it looks for the online help page corresponding to the who command. If this page is found, it is displayed as discussed in the next section.

Making a Shell Script Executable

One of the most important tasks in writing shell scripts is making the shell script executable and making sure that the correct shell is invoked on the script.

In a previous example, you created the logins script that executes the following compound command:

date ; who ;

If you wanted to run the script by typing its name, you need to do two things:

  Make it executable.
  Make sure that the right shell is used when the script is run.

To make this script executable, do the following:

chmod a+x ./logins

Here you are using the chmod command. For a complete discussion of how to use this command, please see Chapter 5, “Manipulating File Attributes.”

To ensure that the correct shell is used to run the script, you must add the following “magic” line to the beginning of the script:

#!/bin/sh

Your script then has two lines:

#/bin/sh
date ; who ;


Previous Table of Contents Next