Redirection


Before we cover viewing files, let's talk about redirection for a minute because I use some redirection in the upcoming section. I cover redirection under shell programming, but for now, I want to give you just a quick introduction to redirection so that we can more effectively cover some of the commands in this chapter.

UNIX is set up such that commands usually take their input from the keyboard, often called standard input , and usually send their output to the screen, often called standard output . Commands also send error information to the screen. It is not always desirable for input to come from standard input and output and errors to go to standard output. You are given a lot of control to override these defaults. This is called redirection. Table 22-1 shows many common forms of redirection.

As shown in the table, to redirect the output of a command from standard output to a file, you would use ">". This works almost all of the time. If you have an environment variable called noclobber set, then redirecting to an existing file does not work (we'll cover environment variables shortly). The noclobber does not permit redirection to write over an existing file. If you try to write over an existing file, such as /tmp/processes below, you receive a message that the file exists:

 graphics/psc_icon.gif #  ps -ef  > /tmp/processes  /tmp/processes:  File exists 

You can, however, use a "!" with redirection to force a file to be overwritten. Using ">!" forces a file to be overwritten, and ">>!" will force the output to be appended to the end of an existing file. Examples of these are shown in Table 22-1.

Table 22-1. Commonly Used Redirection Forms

Command or Assignment

Example

Description

<

wc -l < .login

Standard input redirection:

execute wc (word count) and list number of lines ( -l ) in .login

>

ps -ef > /tmp/processes

Standard output redirection:

execute ps and send output to file /tmp/processes

>>

ps -ef >> /tmp/processes

Append standard output:

execute ps and append output to the end of file /tmp/processes

>!

ps -ef >! /tmp/processes

Append output redirection and override noclobber :

write over /tmp/processes , even if it exists

>>!

ps -ef >>! /tmp/processes

Append standard output and override noclobber :

append to the end of /tmp/processes

(pipe)

ps wc -l

Run ps and use the result as input to wc

- standard input

   

1 - standard output

   

2 - standard error

cat program 2> errors

cat the file program to standard output and redirect errors to the file errors

 

cat program 2>> errors

cat the file program to standard output and append errors to the file errors

 

find / - name '*.c' -print > cprograms 2>errors

find all files on the system ending in .c and place the list of files in cprograms in the current working directory and send all errors (file descriptor 2) to the file errors in current working directory

 

find / -name '*.c' -print > cprograms 2>&1

find all files on the system ending in .c , place the list of files in cprograms in the current working directory, and send all errors (file descriptor 2) to same place as file descriptor 1 ( cprograms )

Using the symbols shown in Table 22-1, you can redirect from standard input and standard output . For instance, rather than display the out put on the screen, you can send the output to file. We will use some of these redirection forms in upcoming examples.



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net