【发布时间】:2011-03-09 16:14:32
【问题描述】:
我希望在 bash 下也将命令的标准输出复制到标准错误。比如:
$ echo "FooBar" (...)
FooBar
FooBar
$
其中 (...) 是重定向表达式。这可能吗?
【问题讨论】:
我希望在 bash 下也将命令的标准输出复制到标准错误。比如:
$ echo "FooBar" (...)
FooBar
FooBar
$
其中 (...) 是重定向表达式。这可能吗?
【问题讨论】:
为了重定向到标准错误,我会使用>&2 或>/dev/stderr。对于复制输出,我会使用tee。它的缺点是需要一个临时文件:
echo "FooBar" | tee /tmp/stdout >&2 ; cat /tmp/stdout
【讨论】:
/tmp/stdout 只是一个例子。随意用您的上下文中更合适的名称替换该文件名。
将 tee 与 /dev/stderr 一起使用:
echo "FooBar" | tee /dev/stderr
或使用 awk/perl/python 手动进行复制:
echo "FooBar" | awk '{print;print > "/dev/stderr"}'
echo "FooBar" | perl -pe "print STDERR, $_;"
【讨论】:
tee /dev/stderr 或 tee >(cat >&2) ?
/dev/stderr 并非在每个发行版上都可用。
tee /dev/stderr 不适用于 systemd 服务,因为 fd 2 是套接字而不是 fifo。这记录在freedesktop.org/software/systemd/man/…。
使用进程替换:http://tldp.org/LDP/abs/html/process-sub.html
echo "FooBar" | tee >(cat >&2)
Tee 将文件名作为参数并将输出复制到该文件。通过进程替换,您可以使用进程而不是文件名>(cat),并且可以将此进程的输出重定向到 stderr >(cat >&2)。
【讨论】:
cat。 (即echo "FooBar" | tee >(>&2))
echo "FooBar" |tee /dev/stderr
tee: /dev/stderr: Permission denied
不适用于 RedHat 6.3
echo "FooBar" | ( read A ; echo $A ; echo $A >&2)
工作中
【讨论】:
如果我可以扩展@defdefred's answer,我正在使用多行
my_commmand | while read line; do echo $line; echo $line >&2; done
它的“优点”是不需要/调用tee 并使用内置函数。
【讨论】: