Previous | Table of Contents | Next |
The magic line causes a new shell (in this case, /bin/sh) to be called to execute the script. Without the magic line, the current shell is always used to evaluate the script, regardless of which shell the script was written for. For example, without a magic line, csh and tcsh users might not be able to get a Bourne shell (sh) script to run correctly.
The Magic of #!/bin/sh
The #!/bin/sh must be the first line of a shell script in order for sh to be used to run the script. If this appears on any other line, it is treated as a comment and ignored by all shells. |
Comments
The magic first line #!/bin/sh introduces the topic of comments. A comment is a statement that is embedded in a shell script but should not be executed by the shell.
In shell scripts, comments start with the # character. Everything between the # and end of the line are considered part of the comment and are ignored by the shell.
Adding comments to a script is quite simple: Open the script using an editor and add lines that start with the # character. For example, to add the following line to the logins shell script:
# print out the date and who's logged on
I opened the file logins with my editor and inserted this line as the second line in the file. The shell script is now as follows:
#!/bin/sh # print out the date and who's logged on date ; who ;
There is no change in the output of the script because comments are ignored. Also comments do not slow down a script because the shell can easily skip them.
You can also add comments to lines that contain commands by adding the # character after the commands. For example, you can add a comment to the line date ; who ; as follows:
date ; who ; # execute the date and who commands
When you are writing a shell script, make sure to use comments to explain what you are doing in case someone else has to look at your shell script. You might find that this helps you figure out what your own scripts are doing, months after you write them.
As you read through this book, you will want to get more information about the commands and features I discuss. This information is available by using the online help feature of UNIX.
Every version of UNIX comes with an extensive collection of online help pages called manual pages. These are often referred to as man pages. The man pages are the authoritative source about your UNIX system. They contain complete information about both the kernel and all the utilities.
To access a man page you need to use the man (man as in manual) command as follows:
man command
Here, command is the name of a command that you want more information about. As an example,
$ man uptime
displays the following on a Solaris machine:
User Commands uptime(1) NAME uptime - show how long the system has been up SYNOPSIS uptime DESCRIPTION The uptime command prints the current time, the length of time the system has been up, and the average number of jobs in the run queue over the last 1, 5 and 15 minutes. It is, essentially, the first line of a w(1) command. EXAMPLE Below is an example of the output uptime provides: example% uptime 10:47am up 27 day(s), 50 mins, 1 user, ⇒load average: 0.18, 0.26, 0.20 SEE ALSO w(1), who(1), whodo(1M), attributes(5) NOTES who -b gives the time the system was last booted.
As you can see this man page is divided into several sections. These sections are described in Table 2.1.
Section | Description |
---|---|
NAME | This section gives the name of the command along with a short description of the command. |
SYNOPSIS | This section describes all the different modes in which the command can be run. If a command accepts arguments they are shown in this section. |
DESCRIPTION | This section includes a verbose description of the command. If a command accepts arguments, each argument will be fully explained in this section |
EXAMPLE | This section usually shows you how to execute a command, along with some sample output. |
SEE ALSO | This section lists other commands that are related to this command. |
NOTES | This section usually lists some additional information about the command. Sometimes it lists known bugs with a particular command. |
Previous | Table of Contents | Next |