10.1. Introduction

 <  Day Day Up  >  

10.1.1 The Steps in Creating a Shell Script

A shell script is normally written in an editor and consists of commands interspersed with comments. Comments are preceded by a pound sign ( # ) and consist of text used to document what is going on.

The First Line

At the top left corner, the line preceded by #! (often called shbang ) indicates the program that will be executing the lines in the script.

 #!/bin/csh or #!/bin/tcsh 

The #! , also called a magic number , is used by the kernel to identify the program that should be interpreting the lines in the script. When a program is loaded into memory, the kernel will examine the first line. If the first line is binary data, the program will be executed as a compiled program; if the first line contains the #! , the kernel will look at the path following the #! and start that program as the interpreter. If the path is /bin/csh , the C shell will interpret the lines in the program. If the path is /bin/tcsh , the TC shell will interpret the lines in the program. This line must be the top line of your script or the line will be treated as a comment line.

When the script starts, the . cshrc file (for csh ) or .tcshrc file (for tcsh ) is read first and executed, so that anything set within that file will become part of your script. You can prevent the .cshrc or .tcshrc file from being read into your script by using the “f (fast) option to the C shell program. This option is written as

 #!/bin/csh -f or #!/bin/tcsh -f 

Note : In all the following examples, if you are using tcsh , change the shbang line to

 #!/bin/tcsh -f 

Comments

Comments are lines preceded by a pound sign. They are used to document your script. It is sometimes difficult to understand what the script is supposed to do if it is not commented. Although comments are important, they are often too sparse or not even used at all. Try to get used to commenting what you are doing not only for someone else, but also for yourself. Two days from now you may not remember exactly what you were trying to do.

Making the Script Executable

When you create a file, it is not given execute permission. You need this permission to run your script. Use the chmod command to turn on execute permission.

Example 10.1.
 1   %  chmod +x myscript  2   % ls -lF  myscript  -rwxr--xr--x    1  ellie  0 Jul  13:00 myscript*  

EXPLANATION

  1. The chmod command is used to turn on execute permission for the user , the group , and others.

  2. The output of the ls command indicates that all users have execute permission on the myscript file. The asterisk at the end of the filename (resulting from the “F option) also indicates that this is an executable program.

An Example Scripting Session

In the following example, the user will create the script in the editor. After saving the file, the execute permissions are turned on with the chmod command, and the script is executed. If there are errors in the program, the C shell will respond immediately.

Example 10.2.
 (The Script - info)     #!/bin/csh -f  # Scriptname: info  1   echo Hello ${LOGNAME}! 2   echo The hour is `date +%H` 3   echo "This machine is `uname -n`" 4   echo The calendar for this month is 5   cal 6   echo The processes you are running are: 7   ps -ef  grep  "^ *$LOGNAME" 8   echo "Thanks for coming. See you soon\!\!"     (The Command Line)     %  chmod +x info  %  info  1  Hello ellie!  2  The hour is 09  3  This machine is jody  4  The calendar for this month is  5  July 2004   S   M   Tu   W   Th   F   S   1   2   3   4   5    6   7    8   9  10   11  12   13  14   15  16  17   18  19   20  21   22  23  24   25  26   27  28   29  30  31  7  The processes you are running are:   < output of ps prints here >  8  Thanks for coming. See you soon!!  

EXPLANATION

1 The user is greeted. The variable LOGNAME holds the user's name . On BSD systems, USER is used. The curly braces shield the variable from the exclamation point. The exclamation point does not need to be escaped because it will not be interpreted as a history character unless there is a character appended to it.

2 The date command is enclosed in backquotes. The shell will perform command substitution and the date's output, the current hour, will be substituted into the echo string.

3 The uname “n command displays the machine name.

4, 5 The cal command is not enclosed in backquotes because when the shell performs command substitution, the newlines are all stripped from the output. This produces a strange -looking calendar. By putting the cal command on a line by itself, the formatting is preserved. The calendar for this month is printed.

6, 7 The user's processes are printed. Use ps “aux for BSD or ps au with Linux. [a]

[a] Linux supports a variety of different style options, including UNIX98, BSD and GNU. See your man page for ps .

8 The string is printed. Note that the two exclamation points are prepended with backslashes. This is necessary to prevent history substitution.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

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