文件重定向详解
上一篇 /
下一篇 2013-04-09 08:54:42
/ 个人分类:SHELL
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=191375
1.command 2>&1 >output.txt 不同于 command >output.txt 2>&1
$ ls x* z* >output.txt 2>&1
$ cat output.txt
ls: cannot access z*: No such file or directory
xaa
xab
$ ls x* z* 2>&1 >output.txt
ls: cannot access z*: No such file or directory
$ cat output.txt
xaa
xab
在第一种情况中,stderr 被重定向到 stdout 的当前位置,然后在将 stdout 重定向到 output.txt,但第二次重定向仅影响 stdout,不影响 stderr。在第二种情况中,stderr 被重定向到 stdout 的当前位置,即 output.txt。注意,在最后一个命令中先重定向标准错误在重定向标准输出,所以标准错误输出仍然打印在终端窗口中。
2.带有 noclobber 选项的输出重定向
$ set -o noclobber #禁止文件被重写。使用 set -o 命令不指定任何选项,可以查看 noclobber 特性是否已经打开
$ ls x* z* >stdout.txt 2>stderr.txt
-bash: stdout.txt: cannot overwrite existing file
$ ls x* z* >|stdout.txt 2>|stderr.txt #>|强制写入
$ set +o noclobber #取消禁止文件被重写
收藏
举报
TAG: