< Day Day Up > |
10.1. Introduction10.1.1 The Steps in Creating a Shell ScriptA 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 LineAt 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 CommentsComments 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 ExecutableWhen 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
An Example Scripting SessionIn 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
|
< Day Day Up > |