topics that matter; ideas worth sharing

share a tip, submit a link, add something new

I/O redirection

June 28, 2002, 12:00 AM —  ITworld — 

Linux allocates three I/O streams for each process: the standard input,
the standard input, and the standard error, which are associated with
the descriptors 0,1 and 2, respectively. By default, the standard output
and the standard error are tied to the user's screen. Thus, if you type
the "cat" command followed by a nonexistent filename, you will see the
following error message on your screen:

$cat nonexistentfile
cat : nonexistentfile not found

It is possible to redirect the standard error and the standard output to
separate destinations. For example, you can direct all error messages to
a file for future reference.

Redirecting Standard Output
To redirect the standard output, use the > operator followed by the
destination file or device. The following cat command direct the output
to a file called mylog:

$ cat intro >mylog

Notice that by default, the > operator redirects the standard output.
Therefore, error messages resulting from the command above will still
appear on the screen. In other words, the command:

$ cat intro >mylog

is interpreted as:

$ cat intro 1> mylog

Redirecting Standard Error
To redirect the standard, use the following command:

$ cat intro 2> errlog

In this case, all error messages are written to the file errlog. If you
wish to redirect error messages to a specific terminal, you can do it
like this:

$cat doc > /dev/tty13

As with other shell commands, you may run the cat command in the
background by appending an ampersand at the end:

$cat doc > /dev/tty13 &

The same effect of redirecting the standard error to a device or a file
can be achieved by using the >> operator too. For example:

$ cat intro 2>> errlog

Redirecting Standard Input
As previously noted, the standard input is associated with the user's
keyboard. You can override this setting by like this:

$ tolowercase

In this example, the program tolowercase reads its input from the file
mydocument rather than the keyboard. Note that the program itself isn't
aware that its standard input has been redirected. Furthermore, the
string " passed in argv. Consequently, every scanf(), getch(), and similar
syscalls will read their data from mydocument.

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources