| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 4 Shell ScriptsIntroduction to Shell Scripts |
|
Stringing commands together on a line with sequential processing, background processing or pipes is an extremely useful tool for a limited number of commands. To save typing the commands repetitively, in the case where you use the same sequence of commands often, you can place the command lines into a file. This file is called a shell script. You create a file containing the commands, tell the system you want the file to be executable (so it can be run as a program), and then type the name of the file to execute the commands in the shell script. A simple shell script could contain the following command line:
which executes each command only when the previous command has completed. To create the script, enter an editor (vi for example) and type the above command line. Save the file. To run the script, you have two methods: the sh command, or changing the permissions on the file. The sh command will create a new shell to run the script. As mentioned in the beginning of this tutorial, it is possible to have several shells running at the same time (with the kernel in control). The sh command creates a new shell to execute the file you specify (if you don't specify a file, it creates a new shell similar to the one you are already in). To execute the script with the sh command, type: sh scriptname where scriptname is the name of the file you placed the command line in. The common way to run a script or program, however, is to declare the file executable with the chmod command. chmod is used to alter the permissions on a file. For our purposes, we will declare the file to be executable by everyone on the system, but only you can update the file. Type: chmod +x scriptname Now the file is executable, and you only need enter the file name to run the script (simply type the scriptname as if it was a command). Your script will execute, and you will see a large output. Both methods of executing scriptname have the same net effect, they just behave differently at first. For details on the chmod command, see the HP-UX Reference. The example above just uses one command line for the script. You can, however, make the script easier to read and contain more than one line of commands. Each line of commands is executed in sequential order (the previous line must complete before the next line is executed). So, we can take the previous example:
and spread the command line into four lines which accomplish the same thing:
When this script is executed, you get the same results as before. If you have a large output from a script like in the above example, you may wish to place some headers or comments in the output and place the output into a file. The echo command will print titles or comments for you. It works in the following manner: echo "string" where string is a string of characters. Modify your example script to look like:
where "\c" causes the next line of output to be printed on the same line, and "\n" causes an extra carriage return and line feed (for more detail see “Echo”). Next you can execute the file using the redirection symbols to append the output to another file. For example, let's say our file is called status1, and the file we wish to place the output in is called status_file:
Each time you monitor the system, you can have the output added to a file. The Bourne Shell runs a script automatically when you login, called .profile. This script sets the "environment" in which you work: it sets up certain variables which tell the system where to look for a command, what the prompt should look like, where to get the mail, and other variables. The .profile file is usually set up by the system administrator, but you can customize it as you learn shell programming techniques. Here is a sample .profile file:
The script sets some essential definitions for shell variables and makes them global to the system. For example, the PATH variable sets up a search path for commands. When you execute a command in the shell, it looks at the PATH parameter. The PATH parameter gives the shell several directories in which to look for the command. If you execute a program that is not in one of the directories specified by PATH, you will receive an error message. Let's go line by line and describe the entries in this sample .profile file:
If you wish to customize your .profile script, you can add any of the items discussed in the shell programming sections. The following are some system parameters and commands you can add to your .profile script which may be of interest:
Here is a list of some system parameters: Table 4-1 Shell Parameters
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||