且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在将 STDOUT 传输到文件时捕获 STDERR 的输出

更新时间:2023-02-22 14:45:33

嗯,在 perl 中也有通用的方法来做到这一点,但是 bash 解决方案(上面让我觉得你正在寻找)是重定向stderr 首先 到标准输出,然后将标准输出重定向到一个文件.直觉上,除非您看到 bash 内部发生了什么,否则这并没有多大意义.但这有效:

Well, there are generic ways to do it within perl too, but the bash solution (which the above makes me think you're looking for) is to redirect stderr first to stdout and then redirect stdout to a file. intuitively this doesn't make a whole lot of sense until you see what's happening internally to bash. But this works:

svnadmin dump $repo -q 2>&1 >$backupFile

但是,不要以另一种方式执行(即,将 2>&1 放在最后),否则 stdout 和 stderr 的所有输出都将进入您的文件.

However, do not do it the other way (ie, put the 2>&1 at the end), or else all the output of both stdout and stderr will go to your file.

你想要的是这个:

# perl -e 'print STDERR "foo\n"; print "bar\n";' 2>&1 > /tmp/f 
foo
# cat /tmp/f
bar

特别是你不想要这个:

# perl -e 'print STDERR "foo\n"; print "bar\n";' > /tmp/f 2>&1
# cat /tmp/f
foo
bar