|
|
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 $1The 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 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../is.logged.in fred
fred console Aug 13 11:28 $./is.logged.in mary
$