< Day Day Up > |
12.1. IntroductionWhen commands are executed from within a file, instead of from the command line, the file is called a shell script and the shell is running noninteractively. Writing Korn shell scripts requires a few steps, as outlined in the following section. 12.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 (#). 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. In Korn shell scripts, this appears as #!/bin/ksh The #!, also called the magic number, is used by the kernel to identify the program that should be interpreting the lines in the script. This line must be the top line of your script. The Korn shell also provides a number of invocation options that control how the shell behaves. These options are listed at the end of this chapter in "Korn Shell Invocation Arguments" on page 740. 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. Executable Statements and Korn Shell ConstructsA Korn shell program consists of a combination of UNIX/Linux commands, Korn shell commands, programming constructs, and comments. Naming and Storing ScriptsWhen naming scripts, it is a good idea to give the script a meaningful name that does not conflict with other UNIX/Linux commands or aliases. For example, you may want to call the script test because it is merely performing some simple test procedure, but test is a built-in command and you may find you are executing the wrong test. Additionally, if you name the file foo, goo, boobar, and so forth, in a few days or even hours you may not have any idea what is in that script! After you have tested your script and found it bug-free, make a directory where you can store the scripts, then set the path so that your scripts can be executed from anywhere in the directory hierarchy. Example 12.1.1 $ mkdir ~/bin 2 $ mv myscript ~/bin (In .profile) 3 export PATH=${PATH}:~/bin 4 $ . .profile EXPLANATION
Making a Script ExecutableWhen you create a file, it is not automatically given execute permission (regardless of how umask is set). You need this permission to run your script. Use the chmod command to turn on execute permission. Example 12.2.1 $ chmod +x myscript 2 $ ls -lF myscript -rwxr--xr--x 1 ellie 0 Jul 12 13:00 myscript* EXPLANATION
Using a Script As an Argument to kshIf you don't make a script executable, you can execute it by passing it as an argument to the ksh command. Example 12.3.
(The Command Line)
$ ksh myscript
EXPLANATION If the ksh program is given a script name as its argument, it will execute the script and the #! line is not necessary or even used. A Scripting SessionIn Example 12.4, the user will create a script in the editor. After saving the file, the execute permissions are turned on, and the script is executed. If there are errors in the program, the Korn shell will respond immediately. Example 12.4.(The Script) 1 #!/bin/ksh 2 # This is the first Korn shell program of the day. # Scriptname: greetings # Written by: Karen Korny 3 print "Hello $LOGNAME, it's nice talking to you." 4 print "Your present working directory is $(pwd)." print "You are working on a machine called $(uname -n)." print "Here is a list of your files." 5 ls # List files in the present working directory print "Bye for now $LOGNAME. The time is $(date +%T)!" (The Command Line) $ chmod +x greetings $ greetings 3 Hello karen, it's nice talking to you. 4 Your present working directory is /home/lion/karen/junk You are working on a machine called lion. Here is a list of your files. 5 Afile cplus letter prac Answerbook cprog library prac1 bourne joke notes perl5 Bye for now karen. The time is 18:05:07! EXPLANATION
|
< Day Day Up > |