Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 4 Shell Scripts

Introduction to Shell Scripts

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

Simple Scripts

Stringing commands together on a line with sequential processing, background processing or pipes is an extremely useful tool for a limited number of commands. To save typing the commands repetitively, in the case where you use the same sequence of commands often, you can place the command lines into a file. This file is called a shell script. You create a file containing the commands, tell the system you want the file to be executable (so it can be run as a program), and then type the name of the file to execute the commands in the shell script.

A simple shell script could contain the following command line:

date; who; ps -ef; du /users

which executes each command only when the previous command has completed. To create the script, enter an editor (vi for example) and type the above command line. Save the file.

To run the script, you have two methods: the sh command, or changing the permissions on the file. The sh command will create a new shell to run the script. As mentioned in the beginning of this tutorial, it is possible to have several shells running at the same time (with the kernel in control). The sh command creates a new shell to execute the file you specify (if you don't specify a file, it creates a new shell similar to the one you are already in). To execute the script with the sh command, type:

sh scriptname

where scriptname is the name of the file you placed the command line in.

The common way to run a script or program, however, is to declare the file executable with the chmod command. chmod is used to alter the permissions on a file. For our purposes, we will declare the file to be executable by everyone on the system, but only you can update the file. Type:

chmod +x scriptname

Now the file is executable, and you only need enter the file name to run the script (simply type the scriptname as if it was a command). Your script will execute, and you will see a large output. Both methods of executing scriptname have the same net effect, they just behave differently at first. For details on the chmod command, see the HP-UX Reference.

Scripts With More Than One Line

The example above just uses one command line for the script. You can, however, make the script easier to read and contain more than one line of commands. Each line of commands is executed in sequential order (the previous line must complete before the next line is executed). So, we can take the previous example:

date; who; ps -ef; du /users

and spread the command line into four lines which accomplish the same thing:

date
who
ps -ef
du /users

When this script is executed, you get the same results as before.

Echo and Redirection in Scripts

If you have a large output from a script like in the above example, you may wish to place some headers or comments in the output and place the output into a file. The echo command will print titles or comments for you. It works in the following manner:

echo "string"

where string is a string of characters.

Modify your example script to look like:

echo "Current date and time: \c"
date
echo "Users logged in:\n"
who
echo "\nCurrent processes:"
ps -ef
echo "\nUser disk usage:"
du /users

where "\c" causes the next line of output to be printed on the same line, and "\n" causes an extra carriage return and line feed (for more detail see “Echo”).

Next you can execute the file using the redirection symbols to append the output to another file. For example, let's say our file is called status1, and the file we wish to place the output in is called status_file:

status1 >> status_file

Each time you monitor the system, you can have the output added to a file.

The .profile File

The Bourne Shell runs a script automatically when you login, called .profile. This script sets the "environment" in which you work: it sets up certain variables which tell the system where to look for a command, what the prompt should look like, where to get the mail, and other variables. The .profile file is usually set up by the system administrator, but you can customize it as you learn shell programming techniques. Here is a sample .profile file:

PATH=/docs/tools:/bin:/usr/bin:/usr/contrib/bin:\
/users/hpux/davek:.
PATH=$PATH:/usr/local/bin:/users/hpux/davek/bin:\
/d1/usr/informix/bin:
PATH=$PATH:/d1/usr/informix/lib:/d1/usr/informix
MAIL=/usr/mail/$LOGNAME
TERM=2623
export TERM PATH MAIL HOME
stty kill '^c'
stty sane
tabs -T$TERM
if mail -e
then
echo
echo "You have mail."
echo
fi

The script sets some essential definitions for shell variables and makes them global to the system. For example, the PATH variable sets up a search path for commands. When you execute a command in the shell, it looks at the PATH parameter. The PATH parameter gives the shell several directories in which to look for the command. If you execute a program that is not in one of the directories specified by PATH, you will receive an error message.

Let's go line by line and describe the entries in this sample .profile file:

  • PATH sets up the search path for the shell. Each directory in the path is separated with a colon (:). Once the shell has read this .profile, any command you execute is searched for first in the /docs/tools directory, then the /bin directory, and so on. Notice the last entry in the first line is a dot (.). This indicates the current directory at the time you execute the command, whatever that happens to be.

    The second and third line are continuations of the PATH parameter. To add to the path, you set the variable PATH to its previous value ($PATH) followed by a colon, then continue listing the directories.

    As you learn more about shell programming and develop several programs, you may wish to call these programs from any directory. One way to do this is to create a "library", a directory which contains all of these shell programs. Then place the path to the library into the PATH variable. This directory will always be searched when you type the program name.

  • MAIL sets the file in which to look for new mail.

  • TERM sets the terminal type. This example is using an HP 2623 Graphics computer terminal.

  • The export command marks parameters for exporting their values to the environment. The export command can be thought of as a way of letting other commands know the value of a variable. If you do not export a parameter, other processes will not know its value.

  • The stty command sets characteristics for your terminal. Setting the kill characteristic to ^c (CTRL-C) tells the computer to interrupt the current process when CTRL-C is pressed.

  • stty sane resets all modes to some predefined reasonable values.

  • tabs will set the tabs to the default format for your terminal. The -T option is followed by the terminal type (here it is $TERM which is a parameter we set earlier to 2623).

  • The last six lines construct a condition (we will learn the details of conditions later). These lines check if you have received any mail. If you have, the message "You have mail." will appear on the screen.

Customizing .profile

If you wish to customize your .profile script, you can add any of the items discussed in the shell programming sections. The following are some system parameters and commands you can add to your .profile script which may be of interest:

  • PS1 is a system parameter which sets the value of the system prompt. The default is $, but you can change that to anything by using the following format:

    PS1 ="string"

    where string is any character string.

  • To have the script clear the screen, include a line with the clear command on it.

  • To have anything printed on the screen, include a line with the echo command:

    echo "string"

    where string is what you want to appear on the screen.

Here is a list of some system parameters:

Table 4-1 Shell Parameters

Parameter

Description

HOME

The default directory for the cd command.

PATH

The search path for commands.

CDPATH

The search path for the cd command.

MAIL

If this parameter is set to the name of a mail file, and the MAILPATH parameter is not set, the shell tells you when mail arrives.

MAILCHECK

This parameter tells how often (in seconds) the shell will check for mail. The default is 600 seconds. If set to 0, the shell will check before each prompt.

MAILPATH

The search path for mail files. The shell informs the user when mail arrives.

PS1

Primary system prompt. The default is "$".

PS2

Secondary system prompt. The default is ">".

IFS

Internal Field Separators, which are normally space and tab.

SHACCT

Write an accounting record in the writable file set by this parameter.

SHELL

If an "r" is contained in the base name (last entry in a path), the shell becomes a restricted shell.

 

NOTE: See “The .profile File” for an example .profile file.
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.