A Sample Shell Script


A common use of shell programs is to assemble an often-used string of commands. For example, suppose you are writing a long article that has been formatted for use with nroff and the related tools tbl and col. When you want to print a proof copy of your article, you have to enter a command string like this:

 $ cat article | tbl | > nroff -cm -rA2 -rN2 -rE1 -rC3 -rL66 -rW67 -rO0 | > col | lp -dpr2

Clearly, typing this entire command sequence, and looking up the options each time you wish to proof your article, is tedious. You can avoid this effort by putting the list of commands into a file and running that file whenever you wish to proof the article. In this example, the file is called proof:

 $ cat proof cat article | tbl | nroff -cm -rA2 \ -rN2 -rE1 -rC3 -rL66 -rW67 -rO0 | col | lp -dpr2

The backslash (\) at the end of the first line of output indicates that the command continues over to the next line. The shell automatically continues at the end of the second line, because a pipe (|) cannot end a command.

Executing Your Script

The next step after creating the file is to make it executable. This means setting the read and execute permissions on the file so that the shell can run it.

If you attempt to run a script that is not executable, you will get an error message like

 sh: proof: Permission denied

To give the proof file read and execute permissions for all users, use the chmod command:

 $ chmod +rx proof

Now you can execute the command by typing the name of the executable file. For example,

 $ ./proof

if the script is in your current directory, or

 $ proof

if it is in a directory in your PATH. At this point, all of the commands in the file will be read by the shell and executed just as if you had typed them.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net