| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 3 Shell CommandSequential Processing |
|
When you enter commands line by line (pressing Return after each command), you are telling the system to complete the command (or program) before executing the next command. Executing:
will complete each command before going on to the next. You can place all of the commands on the same line by using the ";" separator. For example,
is equivalent to entering each command on a separate line. This process is called sequential processing. New programs or commands cannot be started until the preceding program or command has completed. If parameters are required by the program, they are entered as usual. The semicolon is placed after the last parameter. While a program is running as a sequential process, there is no response to keyboard activity until after the program has completed (other than the keyboard buffer delay). Programs already in progress when a program with sequential processing is executed continue to run as usual. While a program is running as a sequential process, you have the option of waiting for the program to finish. Programs can also be run nonsequentially, in which case, each program runs without waiting for the previous program to complete. This type of execution is more commonly called "running in the background." Follow the program name with & to specify background processing.
This example runs program1, program2, and program3, in the background, and returns a prompt to the user immediately. Note that programs that write to the terminal or require input are poor choices for background execution, since the output will be intermixed on the screen, or the input may not be read by the correct program. Every program has at least three data paths associated with it: standard input, standard output, and standard error output. Programs use these data paths to interact with you. By default, standard input (stdin) is your keyboard. The default destination for both standard output (stdout) and standard error (stderr) is your screen. Redirecting input and output is a convenient way of selecting what files or devices a program uses. The output of a program that is normally displayed on the screen can be sent to a printer or to a file. Redirection does not affect the functioning of the program because the destination of output from the program is changed at the system level. The program is unaware of the change. I/O redirection enables you to change a specific data path of a program while leaving its other data paths unchanged. For example, stdout can be stored in a file instead of written to your screen. I/O redirection symbols are entered on the shell command line, or from a shell program. The program begins executing with the data paths specified by the redirection symbols. To specify I/O redirection for a program, each file name is preceded by a redirection symbol, as in:
Spaces between the redirection symbols and the file names are optional. The symbol identifies the name that follows it as a file for input or output. The redirection symbols are listed in Table 3-1 “Redirection Symbols”. Table 3-1 Redirection Symbols
If a file you specify with a redirection symbol is not in the current directory, you should use a path name to identify it. The following actions are taken when the system does not locate files named with the redirection symbols:
Two or more programs or commands can be connected so the output of one program is used as the input of another program. The data path that joins the programs is called a pipe. Pipes allow you to redirect program input and output without the use of temporary files. When programs are connected with pipes, the shell coordinates the input and output between the programs. The pipes only transfer data in one direction, from the standard output of one program to the standard input of another program. The vertical bar (|) is the "pipe" symbol. Parameters for the program are listed after the program name, but before the | symbol. Spacing between the program names and vertical bars is optional. The syntax used for connecting programs with pipes is as follows: program-a | program-b | program-c where program is a command or executable program. Pipes operate on or transform data by separate programs in stages. For example, program-a might require input that you type from the keyboard. program-a could collect this data and then direct it to stdout. This output would be passed through the first pipe to become the input to program-b. program-b might check that data for validity and process it in some way, perhaps sort it. The processed data would then go to stdout and be passed through the second pipe to become the input to program-c. program-c might format that input into a report. Here are some examples. To print the number of files in the current directory, type:
To print a listing of each file in the directory, and paginate it for convenient screen viewing, type:
To send the contents of file to pr, which formats the data and then passes it to lp for printing on the line printer, type:
The redirection symbols can be used for programs connected with pipes. However, only the data paths not connected with pipes can be changed. If you specify a change to a data path being used with a pipe, then an error occurs. The following changes are permitted:
The following commands show how programs can be connected with pipes and how additional changes can be made to data paths with redirection symbols. The first example takes the standard output from test_prog1 and uses it as standard input to /usr/output_prog. test_prog1 | /usr/output_prog The next example runs four programs connected with pipes and puts the output of the fourth program in store_file. get_it | check_it | process_it | format_it > store_file The following pipe uses several of the symbols we just discussed. Try to figure out what will happen before you read the description below. (The backslash (\) at the end of the first line concatenates the two lines into one.)
This pipeline will run three sets of commands sequentially. The first command is to sort the pdir file. When it is completed, the second command set is executed. The parentheses separate the commands so the shell knows which command to associate with a symbol (for more on command grouping, see “Command Grouping”). Therefore, the two commands (pr and sort) are run nonsequentially. So, at the same time, the pdir file is formatted and sent to the printer, and the local file is sorted. Finally, the cat command is run which appends the local file to the pdir file. A helpful way to reduce typing is to use patterns to match file names. If you are in a directory with a file "programming" you can see a listing with either: ls programming or you can use a pattern to match: ls p* where "*" will match any character or string of characters. If you have another file beginning with "p", it too will be listed. Table 3-2 “File Generation Symbols” shows the file generation symbols you can use: Table 3-2 File Generation Symbols
will match a file name that begins with any character a through z (lower case), followed by any single character, followed by the string "cubit", followed by any number of characters, and that ends in ".c" or ".a". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||