Standard Input and Output Redirection

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), write output to standard output (stdout), and write error output to standard error (stderr). By default, standard input is connected to the terminal keyboard and standard output 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 standard output

>&

Redirect standard output and standard error

<

Redirect standard input

>!

Redirect standard output; overwrite file if it exists

>&!

Redirect standard output and standard error; overwrite file if it exists

|

Redirect standard output to another command (pipe)

>>

Append standard output

>>&

Append standard output and standard error



The form of a command with standard input and output 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 redirect output will overwrite any existing file of that name. Setting noclobber prevents this. Using >! and >&! always forces the file to be overwritten. Use >> and >>& to append output to existing files.

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

Examples:

% who > names

Redirect standard output to a file named names

% (pwd; ls -l) > out

Redirect output of both commands to a file named out

% pwd; ls -l > out

Redirect output 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 standard output, is redirected to read myin and write myout:

% myprog < myin > myout 

You can suppress redirected output and/or errors by sending it to the null device/dev/null. The example shows redirection of both output and errors:

% who >& /dev/null 

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

% (cat myfile > myout) >& myerror 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值