您的位置:首页 > 编程语言 > PHP开发

[quote] standard Input and Output Redirection

2013-12-05 21:05 351 查看

From

http://sc.tamu.edu/help/general/unix/redirection.html

Standard Input and Output Redirection

The shell and many UNIX commands take their input from standard input (
stdin
), writeoutput to standardoutput (
stdout
), and write erroroutput to standard error (
stderr
). By default, standard input is connected to the terminal keyboard and standardoutput and error to the terminal screen.

The way of indicating an end-of-file on the default standard input, a terminal, is usually <Ctrl-d>.

Redirection of I/O, for example to a file, is accomplished by specifying the destination on the command line using a redirection metacharacter followed by the desired destination.

C Shell Family

Some of the forms of redirection for the C shell family are:

Character

Action

>


Redirect standardoutput

>&


Redirect standardoutput and standard error

<


Redirect standard input

>!


Redirect standardoutput; overwrite file if it exists

>&!


Redirect standardoutput and standard error; overwrite file if it exists

|


Redirect standardoutput to another command (pipe)

>>


Append standardoutput

>>&


Append standardoutput and standard error

The form of a command with standard input andoutput redirection is:

% command -[options] [arguments]<input file  >output file

If you are using csh and do not have the noclobber variable set, using
>
and
>&
to redirectoutput will overwrite any existing file of that name. Setting noclobber prevents this. Using
>!
and
>&!
always forces the file to be overwritten. Use
>>
and
>>&
to appendoutput to existing files.

Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirectoutput to an existing file without forcing an overwrite, 2) if you redirectoutput to a file you don't have write access to, and 3) if you redirectoutput to a directory.

Examples:

% who >names

Redirect standardoutput to a file named
names


% (pwd;ls-l) >out

Redirectoutput of both commands to a file named
out


% pwd;ls-l >out 

Redirectoutput of
ls
command only to a file named
out


Input redirection can be useful, for example, if you have written a FORTRAN program which expects input from the terminal but you want it to read from a file. In the following example,
myprog
, which was written to read standard input and write standardoutput, is redirected to read
myin
and write
myout
:

% myprog<myin>myout

You can suppress redirectedoutput and/or errors by sending it to the null device,
/dev/null
. The example shows redirection of bothoutput and errors:

% who >& /dev/null

To redirect standard error andoutput to different files, you can use grouping:

% (cat myfile>myout) >& myerror

Bourne Shell Family

The Bourne shell uses a different format for redirection which includes numbers. The numbers refer to the file descriptor numbers (0 standard input, 1 standardoutput, 2 standard error). For example,
2>
redirects file descriptor 2, or standard error.
&n
is the syntax for redirecting to a specific open file. For example
2>&1
redirects 2 (standard error) to 1 (standardoutput); if 1 has been redirected to a file, 2 goes there too. Other file descriptor numbers are assigned sequentially to other open files, or can be explicitly referenced in the shell scripts. Some of the forms of redirection for the Bourne shell family are:

Character

Action

>


Redirect standardoutput

2>


Redirect standard error

2>&1


Redirect standard error to standardoutput

<


Redirect standard input

|


Pipe standardoutput to another command

>>


Append to standardoutput

2>&1|


Pipe standardoutput and standard error to another command

Note that
<
and
>
assume standard input andoutput, respectively, as the default, so the numbers 0 and 1 can be left off. The form of a command with standard input andoutput redirection is:

$ command -[options] [arguments]<input file >output file

Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirectoutput to an existing file without forcing an overwrite, 2) if you redirectoutput to a file you don't have write access to, and 3) if you redirectoutput to a directory.

Examples:

$ who >names

Direct standardoutput to a file named
names


$ (pwd;ls-l) >out

Directoutput of both commands to a file named
out


$ pwd;ls-l >out

Directoutput of
ls
command only to a file named
out


Input redirection can be useful if you have written a program which expects input from the terminal and you want to provide it from a file. In the following example,
myprog
, which was written to read standard input and write standardoutput, is redirected to read
myin
and write
myout
.

$ myprog<myin>myout

You can suppress redirectedoutput and/or error by sending it to the null device,
/dev/null
. The example shows redirection of standard error only:

$ who2>/dev/null

To redirect standard error andoutput to different files (note that grouping is not necessary in Bourne shell):

$ cat myfile>myout2>myerror
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: