Reading input from a file—the < symbol, Writing output to a file—the > and >> symbols, Shell notation for redirecting input and output – HP NonStop G-Series User Manual

Page 136

Advertising
background image

Table 25 Shell Notation for Redirecting Input and Output

Example

Description

Notation

wc <file3

Reads standard input messages from a file.

<

ls >file3

Writes standard output messages to a file.

>

ls >>file3

Adds standard output messages to the end of a
file.

>>

The following subsections describe how to read input from a file and how to write output to a file.

Reading Input From a File—The < Symbol

The OSS shell allows you to redirect the standard input file of a process so that input is read from
a file instead of from the keyboard (the default location) by using the input redirection symbol <
(left angle bracket).

You can use input redirection with any command that accepts input from stdin (your keyboard).
You cannot use input redirection with commands, such as who, that do not accept input.

In the following example, the wc command receives its input from a file (file3):

$ wc < file3

3 27 129

The wc (word count) command counts the number of lines, words, and bytes in the named file.
The output of wc in the example shows that file3 contains 3 lines, 27 words, and 129 bytes. If
you do not supply an operand, the wc command reads its input from the keyboard. In this example,
however, input for wc comes from the file named file3.

If you enter the wc command without the redirection symbol <, you get the following output:

$ wc file3

3 27 129 file3

Writing Output to a File—The > and >> Symbols

The OSS shell allows you redirect the standard output file of a process from the screen (the default
location) to a file. As a result, you can store the text generated by a command in a new or existing
file.

To send output to a file, use either the > (right angle bracket) or the >> symbol in the command
line:

command > targetfile

command >> targetfile

The > symbol causes the shell to:

Replace the contents of the target file with the output of the command, if the target file already
exists

Create the target file, if it does not exist

The >> symbol adds (appends) the output of the command to the end of an existing file. If you
use the >> symbol to write output to a file that does not exist, the shell creates it.

In the following example, the output of ls goes to the file named file:

$ ls > file

If file already exists, the shell replaces its contents with the output of ls. If file does not exist,
the shell creates it.

In the following example, the shell adds the output of ls to the end of the file named file:

136

Managing Processes

Advertising
This manual is related to the following products: