Automating frequent tasks

Creating a shell script

A shell script is a text file containing a sequence of shell commands. The commands are normally entered on separate lines, for readability, but can be separated by semicolons (;).

To create a shell script, create a new file with a text editor (for example vi) and type SCO OpenServer commands into it. Save the file, and use chmod to set the executable permission bit on the file so the shell can run it. For example:


Enter the following text:

who | grep fred

Save the file, and issue the following command:

chmod +x

This assigns the owner of permissions to read, write, and execute the file.

If the current working directory is included in your search path ($PATH), you can execute the file as follows:

   $ .
   fred console Aug 13 11:28
If the file is not held in a PATH directory, an alternate notation is required:
   $ ./
   fred console Aug 13 11:28
The notation ``./'' has the same effect as typing the directory's absolute pathname.

The program runs the command who, to list currently logged in users on the system, then uses grep to search it for the line containing fred, indicating that fred is logged in.

Suppose you want to use the script to see if people other than fred are logged in. You can modify the script as follows:

   who | grep $1
The positional parameter $1 (used in place of fred) refers to the first word on the command line after the name of the script. Where $1 is used in the script, it is substituted for the first argument entered on the command line. You use it like this:
   $ ./ fred
   fred console Aug 13 11:28
   $ ./ mary
fred is logged in; mary is not logged in. This script gives no output if it cannot find the name you supply it with in the output from who.
Next topic: Running a script under any shell
Previous topic: Automating frequent tasks

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005