good good article linux shell简介,especially 第三章输入输出的重定向
http://www.linuxsir.org/main/?q=node/135
分类: shell脚本 |
今天遇到一个古怪的问题:
运行
sudo -u www php a.php > xxxxx
sudo -u www php a.php 2> xxxxx
正确执行;
运行
sudo -u www php a.php 2>&1 >xxxxx
把输出结果保存到xxxx里面了,但并没有把错误也保存起来;
这是我一直没有注意到的地方,也就是说命令写得有问题;
google吧,ft,访问不了;
用yahoo找找吧(刚用几天,还不习惯);
//////////////////////////////////////////////////////////////////////
先看看shell manpage的解释:
REDIRECTION
.................
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.
原来,位置不一样,结果还不一样。汗啊
按照我的理解,第二条命令也应该是对的吧:)
把2重定向到1,再把1重定向到文件,所以2也重定向到文件了;
想不到会这么“奇怪”,也许,shell就是一个怪东西,只要适应了就好了;
Redirecting Output
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.
The general format for redirecting output is:
[n]>word
If the redirection operator is >, and the noclobber option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is >|, or the redirection operator is > and the noclobber option to the set builtin command is not enabled, the redirection is attempted even if the file named by word exists.
看看这个,还可以这样写呢 !不知道是不是只有bash支持。其他shell支持吗?
Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
以后写重定向,得按照这种格式写了:
>word 2>&1
运行
sudo -u www php a.php > xxxxx
sudo -u www php a.php 2> xxxxx
正确执行;
运行
sudo -u www php a.php 2>&1 >xxxxx
把输出结果保存到xxxx里面了,但并没有把错误也保存起来;
这是我一直没有注意到的地方,也就是说命令写得有问题;
google吧,ft,访问不了;
用yahoo找找吧(刚用几天,还不习惯);
//////////////////////////////////////////////////////////////////////
先看看shell manpage的解释:
REDIRECTION
.................
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.
原来,位置不一样,结果还不一样。汗啊
按照我的理解,第二条命令也应该是对的吧:)
把2重定向到1,再把1重定向到文件,所以2也重定向到文件了;
想不到会这么“奇怪”,也许,shell就是一个怪东西,只要适应了就好了;
Redirecting Output
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.
The general format for redirecting output is:
[n]>word
If the redirection operator is >, and the noclobber option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is >|, or the redirection operator is > and the noclobber option to the set builtin command is not enabled, the redirection is attempted even if the file named by word exists.
看看这个,还可以这样写呢 !不知道是不是只有bash支持。其他shell支持吗?
Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
以后写重定向,得按照这种格式写了:
>word 2>&1