DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
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:

vi is.logged.in

Enter the following text:

who | grep fred

Save the file, and issue the following command:

chmod +x is.logged.in

This assigns the owner of is.logged.in 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:

   $ . is.logged.in
   fred console Aug 13 11:28
If the file is not held in a PATH directory, an alternate notation is required:
   $ ./is.logged.in
   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:
   $ ./is.logged.in fred
   fred console Aug 13 11:28
   $ ./is.logged.in 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

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003