Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 3 Shell Command

Sequential Processing

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

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:

date
ps -ef
who

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,

date; ps -ef; who

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.

Nonsequential (Background) Processing

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.

program1 & program2 & program3 &

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.

Redirecting Input and Output

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.

How to Redirect Input and Output

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:

programA < filename
programB > filename

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

Symbol

Function

Example

<

Read standard input from an existing file.

program1 < input.data

>

Write standard output to a file.

program2 > output.data

>>

Append standard output to an existing file.

sample.prog >> output.data

 

NOTE: Using > destroys any previous contents of the file specified to receive the output. If a file's contents must be preserved, use >>.Be careful not to use the same file for standard input and standard output. When input and output operations access the same file, the results are unpredictable.

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:

  • If a file specified for input with the < symbol is not located, an error message is displayed.

  • If a file specified for output with the > or >> symbol is not located, it is created and used for program output.

Examples

The following examples show how the data paths of programs, commands, or utilities can be modified with the redirection symbols.

CHItest < data1

Runs the program CHItest using the file data1 as input.

date >> syslog

Adds the current time and date to the end of the file syslog.

Pipes

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.

How to Connect Programs With Pipes

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:

ls | wc

To print a listing of each file in the directory, and paginate it for convenient screen viewing, type:

ls | more

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:

cat file | pr | lp

Redirection in Pipes

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 standard input of the first program using a pipe can be redirected with the < symbol.

  • The standard output of the last program using a pipe can be redirected by using the > symbol or appended to an existing file with the >> symbol.

Examples

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

Pipe Example

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.)

sort pdir; ( ( pr pdir | lpr )& \
(sort local)& ); cat local >>pdir

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.

File Name Generation

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

Symbol

Description

*

Matches any string of characters including the null string.

?

Matches any single character.

[ ... ]

Matches any one of the characters enclosed in the brackets. A pair of characters separated by a minus will match any character between the pair (lexically).

 

[a-z]?cubit*.[ca]

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".

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.