11.1. 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.

11.1.1. 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. This line is commonly:

#!/bin/tcsh

The #! is called a magic number and 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/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 .tcshrc file is read first and executed, so that anything set within that file will become part of your script. You can prevent the .tcshrc from being read by using the -f (fast) option to the TC shell program. The startup line is written as follows:

#!/bin/tcsh -f

11.1.2. 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.

11.1.3. 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 11.1.
1   > chmod +x myscript
2   > ls-F myscript
    -rwxr--xr--x    1  ellie0 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 joker file. The asterisk at the end of the filename (resulting from the -F option) also indicates that this is an executable program.

11.1.4. 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 11.2.
 (The Script - info)
    #!/bin/tcsh -f
    # This script is called 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 au | grep  "^ *$LOGNAME"
8  echo "Thanks for coming. See you soon!!"
   (The Command Line)
9  >  chmod +x info
10 > ./info
						1  Hello ellie!
						2  The hour is 09
						3  This machine is jody
						4  The calendar for this month is
						5      January 2000
						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 back quotes. 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. The cal command is not enclosed in back quotes 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.

5. The calendar for this month is printed.

6,7. The user's processes are printed.

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

11.1.5. Variables (Review)

To write shell programs, you will use variables to hold information. The values will be assigned either directly to the variable in the script, or passed in from the command line, or retreived as user input. See "Variables" on page 561 and "Environment Variables" on page 569 for a complete description of local and environment variables.

Example 11.3.
 
set name = "Ellie"   # local variable assignment
setenv NAME "Tom"    # environment variable assignment
echo $name           # printing value of local variable
echo $NAME           # printing value of environment variable

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.235.144