首页 > 编程知识 正文

应用重定向,shell输出重定向到文件

时间:2023-05-04 21:09:38 阅读:147914 作者:2175

标准hsdmt/输出重定向:

sandardinputandoutputredirection

theshellandmanyunixcommandstaketheirinputfrom

Sandardinput(stdin ),write output to

标准输出(stdout ),and write error

outputtostandarderror(stderr ).By default,

sandardinputisconnectedtotheterminalkeyboardandstandard

outputanderrortotheterminalscreen。

thewayofindicatinganend-of-fileonthedefaultstandard

input,a terminal,is usually。

Redirection of I/O,for example to a file,is accomplished by

specifyingthedestinationonthecommandlineusinga

redirectionmetacharacterfollowedbythedesire

目的。

c -壳族

someoftheformsofredirectionforthecshellfamilyare :

角色

操作

注册标准

output

redirectstandardoutputandstandarderror

注册标准输入

注册标准输出; overwrite file if it exists

redirectstandardoutputandstandarderror; overwrite文件if

it exists

redirectstandardoutputtoanothercommand (pipe ) )。

附加标准输出

appendstandardoutputandstandarderror

theformofacommandwithstandardinputandoutputredirection

is:

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

ifyouareusingcshanddonothavethenoclobber

variable set,using and to

redirectoutputwilloverwriteanyexistingfileofthatname。

settingnoclobberpreventsthis.using!

和! always forces the file to be

overwritten. Use and

到to append output to existing files。

redirectionmayfailundersomecircumstances :1 ) if you have

thevariablenoclobbersetandyouattempttoredirect

outputtoanexistingfilewithoutforcinganoverwrite,2 ) if you

redirectoutputtoafileyoudon ' thavewriteaccessto,and 3) if

you redirect output to a directory。

Examples:

% who names

redirectstandardoutputtoafilename

d 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

The Bourne shell uses a different format for redirection which

includes numbers. The numbers refer to the file descriptor numbers

(0 standard input, 1 standard output, 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

(standard output); 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 standard

output

2>

Redirect standard error

2>&1

Redirect standard error to standard output

<

Redirect standard input

Pipe standard output to another command

>>

Append to standard output

2>&1|

Pipe standard output and standard error to another command

Note that < and > assume

standard input and output, respectively, as the default, so the

numbers 0 and 1 can be left off. The form of a command with

standard input and output 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 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

Direct standard output to a file named names

$ (pwd; ls -l) > out

Direct output of both commands to a file named

out

$ pwd; ls -l > out

Direct output 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 standard output, is

redirected to read myin and write

myout.

$ myprog < myin > myout

You can suppress redirected output and/or error by sending it to

the null device, /dev/null. The example shows

redirection of standard error only:

$ who 2> /dev/null

To redirect standard error and output to different files (note

that grouping is not necessary in Bourne shell):

$ cat myfile > myout 2> myerror

原文(http://sc.tamu.edu/help/general/unix/redirection.html)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。