原帖由
皇家救星 于 2009-1-8 22:59 发表

cmd > output.txt
这种情况需要本身cmd命令就会把错误提示传进标准错误,正确结果传进标准输出
例如我用shell写一个cat命令就不知道在源文件不存在的时候怎么将“源文件不存在”这一行信息传给标准错误
...
#!/bin/sh
#write to stderr
exec 4>&1 #dup 4 as stdout, i.e. backup stdout
exec >&2 #redirect stdout as stderr
echo "stderr, hello"
echo "stderr, world"
exec >&4 #restore stdout
exec 4>&- #close fd 4
#write to stdout
echo "stdout, hello world"
$ ./test.sh
stderr, hello
stderr, world
stdout, hello world
$ ./test.sh >out
stderr, hello
stderr, world
$ cat out
stdout, hello world