< Day Day Up > |
14.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. When the bash (Bourne Again) shell starts running noninteractively, it looks for the environment variable, BASH_ENV (ENV) and starts up the file (normally .bashrc) assigned as its value. After the BASH_ENV file has been read, the shell will start executing commands in the script.[1]
14.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, commonly called the shbang line, is written as #!/bin/bash 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 bash program can also accept arguments to modify its behavior. See Table 14.8 on page 951 for a list of bash options.
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 bash Shell ConstructsA bash shell program consists of a combination of UNIX/Linux commands, bash 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 14.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 will create a script in the editor. After the user saves 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 14.2.(The Script) 1 #!/bin/bash 2 # This is the first Bash shell program of the day. # Scriptname: greetings # Written by: Barbara Bashful 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) $ greetings # Don't forget to turn turn on x permission! bash: ./greetings: Permission denied. $ chmod +x greetings $ greetings or ./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 > |