< Day Day Up > |
Shell programs are usually called shell scripts . Now that you have learned how to use the shell interactively, you are ready to start scripting using what you have learned in addition to the programming constructs provided in this chapter. It doesn't matter whether you are a system administrator, a programmer, or a user . Shell scripts can automate a number of routine tasks to make life a lot easier ”and they are fun to write and use! 8.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 LineThe first line at the top left corner of the script will indicate the program that will be executing the lines in the script. This line, known as the shbang line, is commonly written as #!/bin/sh 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. This line must be the top line of your script. CommentsComments are lines preceded by a pound sign and can be on a line by themselves or on a line following a script command. 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 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 recall exactly what you were trying to do. Executable Statements and Bourne Shell ConstructsA Bourne shell program consists of a combination of UNIX commands, Bourne shell commands, programming constructs, and comments. Making the Script ExecutableWhen you create a file, it is not given the execute permission. You need this permission to run your script. Use the chmod command to turn on the execute permission. Example 8.1.1 $ chmod +x myscript 2 $ ls -lF myscript -rwxr-xr-x 1 ellie 0 Jul 13:00 myscript* EXPLANATION
A Scripting SessionIn the following example, the user creates 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 shell will respond immediately. Example 8.2.(The Script) 1 #!/bin/sh 2 # Scriptname: greetings # Written by: Barbara Born # This is the first Bourne shell program of the day. 3 echo "Hello $LOGNAME, it's nice talking to you." 4 echo "Your present working directory is `pwd`." echo "You are working on a machine called `uname -n`." echo "Here is a list of your files." 5 ls # List files in the present working directory 6 echo "Bye for now $LOGNAME. The time is `date +%T`!" (The Command Line) 7 $ chmod +x greetings $ greetings 3 Hello barbara, it's nice talking to you. 4 Your present working directory is /home/lion/barbara/prog 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 6 Bye for now barbara. The time is 18:05:07! EXPLANATION
|
< Day Day Up > |