| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 8 Detailed ReferenceCommand Separators |
|
When you execute commands in a shell program separated by newlines (Returns), the commands are executed sequentially or in the order they appear in the file. The following separators allow you to control the sequence of command execution. This separator is a conditional separator. It will execute the next command in the command line only if the previous command executes successfully.
This command line will first test to see if /users/rhonda/tools is a directory. If it is, the cd command is executed. If not, no further action is taken. The double vertical bar separator will execute the next command only if the previous command was unsuccessful.
This command line will test to see if the directory /users/michael/projects exists. If the test fails, the echo command is executed. Here is an example which mixes the above separators:
The shell uses ; and & to terminate a command sequence. Thus there are two command sequences: test -d /tools &&cd /tools, and test -z "$fn" || sort -o $fn $fn. The first sequence is executed before the second (because of the ; separator). If the first test is successful, the cd command is executed. The second command sequence is then executed in the background (due to the terminating &). The second test is performed, and if unsuccessful, the sort is performed. You can group a sequence of commands together using parentheses () or braces {}. If you group a series of commands with parentheses, a subshell is created to run the commands.
This command grouping is executed separately from the current shell program. The current shell program only sees the results of the command grouping. The advantage of command grouping is you can place a series of commands in the background, or use other command separators to achieve a variety of results. Here's another example:
This command sequence will test if "file" is an ordinary file. If it is, it runs a command grouping in the background (note the terminating &). This command line could be simplified to read:
Another helpful way to group commands is with braces ({}). This command grouping is used primarily for redirecting combined output. You can group a series of commands together and use the resulting output:
All of the commands in the braces are executed, and the resulting output from all of the commands is placed in a file called contents.
The more complicated your shell programs get, the more you will want to modularize them by using functions. This way you can create generic functions which can be re-used and eliminate repetitive code. To define a function, use the following syntax: name () { list; } where name is the name of the function, and list is a list of commands used in the function. Here is an example to show how functions are defined:
This function tests the file name to see if it is a directory. If it is, it returns a status of 0 (see “Return Values”). Otherwise, it returns status 1. Do not forget to place the semicolon (;) at the end of the last line. You can type your function in its entirety at the beginning of the shell program. When you wish to access it, you use the following format: name [parameter]... where name is the name of the function, and [parameter]... refers to any optional positional parameters you wish to include. You should note the following things concerning Bourne Shell functions:
The common redirection symbols can be used in shell programs (> for redirecting output to a file, >> for appending output to a file, < for redirecting input to a command from a file). In addition are these redirection conventions: << [-]word This redirection construct, called a here document, causes all lines after this one, and up to a line consisting only of word, to be used as input data. Let's look at a sample section from a script file:
The text down to (but not including) "marker" will be printed on the screen when this script is run. Then the echo command is executed, giving an output of:
Be sure to include quotes in word if the line contains special characters for command and parameter substitution, because they will be interpreted if not quoted. Notice it does not just look for the word "marker" but rather the line containing only the word "marker". << is particularly useful for multiline input to commands (usually ed(1) commands). It is also useful because it eliminates, in many cases, the need for separate input files. If you add the optional - after <<, then all leading tabs in the here document are stripped. <&digit This input redirection symbol uses the file descriptor associated with the descriptor digit. Most programs have standard input as 0, standard output as 1, and standard error as 2 (stdin, stdout, and stderr respectively). All programs which work properly with pipes observe 0 and 1 (and consequently 2). Other programs may not. >&digit is the format for using descriptors, where digit can be any single digit (0-9). The most commonly used redirection of this form is 1>&2 or 2>&1. For example,
The output of this line is redirected to the standard error (your terminal). So, in effect, you are creating your own error message and redirecting it in the same manner as an error from the shell. You can use this capability to ensure messages in a shell file reach the user. In the same manner 2>&1 merges the standard error into the standard output. And, you can use the <& capability in a similar manner to use as standard input. The order in which you place the redirections is significant. The shell evaluates redirections from left to right:
will first associate file descriptor 1 (thus, it is no longer associated with standard output) with the file fileA. Then, file descriptor 2 is associated with the file with file descriptor 1 (which is fileA). If we had placed the 2>&1 first, file descriptor 2 would be associated with file descriptor 1 (the terminal), and then file descriptor 1 would be associated with fileA. To force both the standard output and error output into the same file, you usually use a statement like:
To close standard input use: <&-. To close standard output use: >&-. The following are commands which may be of help to you in your shell programs. The exec command allows you to replace the current shell with a new shell or another program. With the syntax: exec [arg]... where arg can be a sequence of commands or shell programs. This command can be helpful in cases where you do not wish to create subshells. You could have no desire to return to the parent shell, or you may be recursing and do not wish to keep parent shells active. A good example is a shell script which calls itself. The expr command is very useful for performing arithmetic operations in shell programs. It also has other operations useful for string manipulation. With the form: expr expression {+|-} expression you can add or subtract integers.
will return the string 20. To modify variables, you can use a similar format to:
using command substitution (grave accents) to place the new value in the variable a. The symbols for multiplication, division, and remainder of integer-valued arguments are: \*, /, and %, respectively. Note the * is preceded by a backslash (\) to escape the shell's interpretation of the asterisk. To compare integers, use the following format: expr expression {=|\>|\>=|\<|\<=|!=} expression where != is "not equal to", and the other symbols represent mathematical comparisons (again, note the backslash before the special characters < and >). The function will return 0 if the comparison is successful and 1 if it is not. Here is an example of how a comparison might be used:
expr expression \| expression will return the first expression if it is neither null nor 0. Otherwise, it will return the second expression. expr expression \& expression will return the first expression if neither expression is null nor 0. Otherwise, it will return 0. expr can also be used in string manipulation (the strings can be arithmetic): expr expression : expression will compare the first argument with the second argument, which must be a regular expression. (There is a discussion of Regular Expressions in Text Processing: User's Guide and in The Ultimate Guide to the vi and ex Text Editors.) The ^ symbol is not a special character, however, because all patterns are anchored (begin with "^"). Normally, the matching operator returns the number of characters matched, and 0 on failure. expr length expression will return the length of the expression (number of characters). expr substr expression expression expression will return a substring of the first expression, starting at the character specified by the second expression, and for the length given by the third expression. For example:
returns the string "bat". And,
returns the string "man". (Note that the second and third expressions must be numeric.) expr index expression expression will return the position in the first expression which contains a character found in the second expression:
returns the value 4. The set command has a variety of uses. It is mainly used to set the value of a parameter. Let's begin with using set without arguments. If you type set, you get a list of all the parameters the system knows. These will include system parameters set by your .profile file, and any parameters you define. You can define, or set, positional parameters easily with set. Simply follow the set command with the values for the positional parameters $1, $2, and so on. Here is an example: set camp town ladies Now $1 has the value "camp", $2 has the value "town", and $3 has the value "ladies". You can also use command substitution with the set command: set `date` where $1="Thu", $2="Jun", and so on for a date output of "Thu Jun 26 09:34:01 MDT 1986". There are several options you can use with set. Preceding the option with - will turn the flag on. Preceding the option with + will turn the flag off. The format is as follows: set [[--aefhkntuvx] [arg]...] where the options are shown in the following table. Table 8-1 Options to the set Command
These options can also be used with the sh command. This command will remove the specified variable or function. The format is: unset [name]... where name is a list of variables or functions except PATH, PS1, PS2, MAILCHECK, or IFS. The trap command waits until signals are sent to the shell program, and traps them. Instead of performing the default action, you can have the signals processed any way you wish. In other words, you use the trap command to wait for certain signals from the shell (which may be an unsuccessful command execution). When the trap sees a signal, it executes a list of predefined commands you generate. The syntax is: trap [command_list] [n]... where n is the signal (or signals) trap looks for. When they are found, command_list is executed. If n is 0, then the command list is executed when the shell is exited. If you type trap with no arguments, a list of commands associated with each signal number is printed. An attempt to trap signal 11 (memory fault) produces an error. Table 8-2 “Signals” is a list of the signal numbers, their descriptions, and whether they can be trapped. To trap for signals 0, 1, 2, 3, 15 and execute a certain set of commands, you would use a command similar to:
Signals 1, 2, and 3 cannot be trapped if the script is run in the background (using nonsequential processing symbol '&'). Signal 9 should not be used as an argument to trap because it can never be caught, as well as signal 14 (it is used internally by sh(1)). Table 8-2 Signals
The format for hash is: hash [-r] [name]... where name... is a list of command names. The hash command makes searching for a command faster. Usually the shell will look in your search path (indicated in the shell parameter PATH) and go through each directory searching for the first occurrence of the command. hash will place the command in a table and include a pointer to the directory in which it resides. Thus, when you call the command, the hash table is first checked. If the command is in the hash table, it will be able to go directly to the directory instead of through all of the directories in the search path. If you wish to delete the remembered locations in the hash table, include the -r option. The default for hash (no options or parameters) is to print a listing of all commands used since login. The list includes two columns: hits which are the number of times the command has been invoked by the shell process, and cost which is the measure of work required to locate a command in the search path. The default hash command is used more for information, to see how the performance of the hash table is compared to the search path. If you wish to see if a command is in a hash table, you can use the type command. The type command will tell you where a command is located in the directory structure. It will also indicate if the command is hashed (see hash above). The format is: type [name]... where name... is a list of commands. The readonly command is used to set the value of a parameter permanently. The format is as follows: readonly [name]... where name... is a list of parameters. When you use the readonly command on a parameter, it places the parameter into a set of parameters which are marked so they cannot be changed. No attempts to change the value of the parameter are allowed. For example, let's say we specify these parameters to be readonly:
If we attempt to change the value of, say, dogs,
we get the message:
and the value remains at "rover". If you type in readonly with no parameters (the default), you get a list of all parameters which are readonly:
You can change your group identification with newgrp. You remain logged in, but access permissions to files are done according to the new group environment. With newgrp you are always given a new shell even if the command terminates unsuccessfully. The format is as follows: newgrp [-] [group] where group is the new group, and the - option will cause the environment to be changed to what it would be if you logged in again (you lose your old shell and get a new one). With no arguments, the group is changed back to what your password entry file indicates. For more information, see newgrp(1) in the HP-UX Reference. This command prints the accumulated user and system times for processes run from the shell. The times are precise to units of 1/HZ seconds, where HZ is processor dependent. The output looks like: 0m37s 0m25s For more information, read times(2) in the HP-UX Reference. This command provides control over process limits. The format is: ulimit [-fp ] [n] where n is the size limit imposed by ulimit. The -f option imposes a size limit of n blocks on files written by child processes (with no argument the current limit is printed). The -p option changes the pipe size to n. If no option is given, the -f option is assumed. The wait command will wait until the specified process is finished, and then report its termination status. To specify the process, use this format: wait [n] where n is the process ID. Most of the time you will not know the process number, but if you look ahead to the section “Parameters Set by the Shell”, you will notice one entry (!) refers to the process number of the last background command executed. So, to wait for that background process to terminate, you would use: wait $! wait without parameters waits for all child processes to terminate. When a function or a command terminates, it sets a flag indicating the status of the termination. In other words, if the function or command was successful in executing, it returns a value indicating its success. The values (or error codes) normally used are listed in the exit section. These values are only conventions; shell scripts normally use these conventions, but programs in general do not. When you execute a shell command incorrectly, you usually get an error message. What usually happens is the shell command returns an error code. If the error code is, say, 2, you will receive a message indicating a syntax error has occurred. You can return error codes from your shell programs and functions in two ways. The exit statement can return any value you specify by using the following format: exit n where n can be an integer from 0 to 255. You can return error codes from functions by using the return statement: return n To check the return value of the last command you executed, you can use a parameter called $?. You can access several special parameters that are set automatically by the shell. As mentioned in “Return Values”, $? holds the return value of the executed command or function. Other such parameters you can use are: Table 8-3 Parameters Set by the Shell
If the sh command is used to invoke shells or shell programs, you have several options available. You can use the options in Table 8-4 “Options for sh Command”, and you can also use the options described in Table 8-1 “Options to the set Command”. Table 8-4 Options for sh Command
Making a shell restricted (or rsh) causes the following actions to be disallowed:
The restricted shell is useful when you wish users to have limited access to the system. Make sure the directories in which the restricted users are placed do not give them access to subdirectories in which they may do damage. Also make sure they do not have commands available to them, such as chshand csh, that let them escape the restricted shell. (See sh(1) about provisions to ensure this.) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||