| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 14 Commands, Jobs, and ScriptsC Shell Scripts |
|
Shell scripts are files containing a series of commands that the shell executes as a group. The files .login, .cshrc and .logout are all shell scripts. While shell scripts are a valuable programming and operating aid, there are some situations where scripts are not useful. Many excellent commands and program libraries are provided with HP-UX. Before writing a script, check your HP-UX Reference. A solution to your problem may already exist. A C Shell command script may be executed by typing in: csh script_one [arg_1 [arg_2]...] where script_one is the name of the shell script file to execute, and arg_1, arg_2, ... is a list of optional arguments that may be required by the script. C Shell places these arguments in the shell variable array argv as argv[1], argv[2], etc. There is no argv[0]. (C Shell uses $0 to refer to argv[0] instead.) In this example, $0 equals script_one. C Shell then begins to sequentially read the commands from script_one. If you want to be able to execute the script file directly without beginning the command line with csh, edit the script file so that the first line is #!/bin/csh. The pound sign is also used for comment lines in the script. Next, use the chmod command to make the file executable. For example:
makes script_one executable and readable for everyone and writable by you. For more information on the chmod command, see chmod(1) in the HP-UX Reference. Now, when you type:
C Shell automatically executes the shell script file script_one. If the first line in the file is not #!/bin/csh, the Bourne shell will attempt to execute the shell script file instead. C Shell parses each shell script line into command arguments. Each distinct command is identified, and variable substitution is performed. Keyed by the dollar sign character ($), this substitution replaces the names of variables by their values. Thus,
when placed in a command script, echoes the current value of the variable sum1 to the shell script's standard output file. An error results if sum1 has no value assigned. To discover if a variable has a value currently assigned to it, use the notation
The question mark (?) causes the expression to return the value one (1) if the variable has a currently assigned value and zero (0) if not. This is the only available method for accessing a variable that does not have an assigned value without generating an error. To determine how many component variables have been assigned to a variable, use the notation
The pound sign (#) notation returns the number of component variables assigned to the specified variable. For example,
You can readily access the individual components of a variable that has several assigned values. Thus,
echoes the first component variable of sum1. In the example above a is echoed. Similarly,
returns the component variable sum1, which is "c".
returns both a and b. Other notations useful in shell scripts include
(where n is a number), which is a shorthand equivalent of
and returns the nth component variable of argv. Another is
which is a shorthand for
One minor difference between $n and $argv[n] should be noted. $argv[n] will yield an error if n is not in the range 1 through $#argv while $n will never yield an out-of-range subscript error. This is for compatibility with the way other shells handle parameters. One way to avoid this type of error is to use a subrange of the form n-. If there are fewer than n component variables for the given variable, an empty vector is returned. A range of the form m-n also returns an empty vector without giving an error when m exceeds the number of elements of the given variable, provided the subscript n is within range. The form
expands to the process number of the current shell. Each process is unique, so the process number can be used to generate unique temporary file names. The form
is replaced by the next line of input read from the shell's standard input, instead of using the next line in the script being processed. This is useful when writing interactive shell scripts. For example,
would write the prompt "yes or no?" to the shell's standard output device and then read the answer from the shells standard input device into the variable a.
Construction of useful shell scripts requires that it be possible to evaluate expressions in the shell based on the current values of certain variables. In fact, most C language arithmetic operations are available in the shell with the same precedence that they have in C. In particular, the operations == and != compare strings, while the operators && and || implement the boolean AND/OR operations. The special operators =~ and !~ are similar to == and != except that the string on the right side can have pattern-matching metacharacters (like *.? or []) and the test is whether the string on the left matches the pattern on the right. The shell also allows file inquiries of the form -? filename where ? is replaced by a number of characters. For example the expression primitive -r filename tells whether the file filename exists and is readable. The expression is true if filename exists. Other primitives test for read, write and execute access to the file, whether it is a directory or ordinary file, and test for nonzero length. See test(1) in the HP-UX Reference for specifications of these primitives. You can determine whether a command terminated normally by enclosing it in braces ({}).
This notation returns a one (1) if the command terminated normally with exit status 0, or a zero (0) if the command terminated abnormally or with a nonzero exit status. If more detailed information about the execution status of a command is required, the command can be executed and the system variable status examined in the next command. Remember, however, that status is set by every command, so it is very transient. As an example using the normal termination condition, consider the following command line:
or, similarly, the shell script:
For a complete list of expression components available for shell scripts, see csh(1) in the HP-UX Reference. Control structures allowed by C Shell are similar to those in the C programming language. Comment your script using the pound sign (#) at the beginning of each comment line or command line that is to be ignored during execution. The syntax for this statement is: foreach index_variable ( loop_count_value_list ) All of the commands between the foreach line and its matching end line are executed for each value in loop_count_value_list . The variable index_variable is set to the successive values of loop_count_value_list. Within this loop, the break command can be used to stop loop execution, while the continue command can be used to prematurely terminate one iteration and begin the next. Upon completion of the foreach loop, the value of the iteration variable index_counter is the same as it was during the last loop in loop_count_value_list. This command has the following syntax: if ( expression ) then Keyword placement is not flexible here due to current shell implementation. That means the control structure has to be exactly as shown. In other words, if and then must be in the same line and endif must be in a separate line. You can nest these statements using the keyword else. For example: if ( expression ) then Note that only one endif is used to end the entire structure. C Shell has another form of the if statement: if ( expression ) command This can also be written: if ( expression ) \ If you only need to execute one command, the endif statement can be omitted. In the second example, the nonprinting newline character is escaped with the backslash (\) to allow the command to appear below the expression. This is to improve visual clarity. The while structure is like that found in the C programming language. For example: while ( expression ) The switch structure is like that found in the C programming language. For example: switch ( word )
By default, commands run from shell scripts use the standard input of the shell which is running the script. This is different from how other shells run under HP-UX. This allows C Shell shell scripts to fully participate in pipelines, but extra notation is required for commands which use inline data. Thus we need a metanotation for supplying inline data to commands in shell scripts. For example, consider this script which runs the editor to delete leading blanks from the lines in each argument file. (The space and the tab in the example represent the space and the tab characters.)
The notation << 'EOF' means that the standard input for the ed command is to come from the text in the shell script file up to the next line consisting of exactly EOF. The fact that the 'EOF' is enclosed in single-quote characters causes the shell to not perform variable substitution on the intervening lines. In general, if any part of the word following the << which the shell uses to terminate the text to be given to the command is quoted then these substitutions will not be performed. In this case since we used the form 1,$ in our editor script we needed to insure that this $ was not variable substituted. We could also have insured this by preceding the $ here with a \, that is:
but quoting the 'EOF' terminator is a more reliable way of achieving the same end. If our shell script creates temporary files, we may wish to catch interruptions of the shell script so that we can clean up these files. To do this, start your script with onintr label where label is a program label marking the code that handles the interrupt condition. If an interrupt is received by the shell, C Shell will do an automatic goto label and execute the desired code. If you wish to exit your program with a nonzero status, make exit 1 a part of your interrupt handling code. This script backs up a list of C programs only if they are different from previously backed up versions. The files are stored in your home directory in the subdirectory backup. It makes use of the foreach statement to execute all of the commands between the foreach statement and its matching end. The script might be invoked with: bkupscript *.c.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||