2014-01-23 130 views
0

我想仅使用/ dev/tty写入stderr。如果我直接写入/ dev/tty(使用tee),那么似乎会在stdout上打印出来。那是对的吗?我如何指定要打印到标准错误?使用/ dev/tty写入stderr?

目前在bash线看起来像

echo "foo" >&2 | tee /dev/tty | logger -it "my_script" 
+0

你有一个在stdout和stderr上产生输出的命令,你想记录stderr并在屏幕上显示它? –

+2

也许你真正想要的是'echo“foo”| tee/dev/stderr | logger -it“my_script”' – anishsane

+1

你可能会觉得这个答案有帮助:http://stackoverflow.com/a/692407/1608708 – imp25

回答

1

如果我们的#

echo "foo" >&2 # echo "foo" and redirect to fd 2 (/dev/sdterr) 
| #pipe stdout to 
tee /dev/tty #both send stdout to file /dev/tty, which is terminal file that can output both stdout and stderr depending on what you pass to it (so you probably want /dev/stdout/ or /dev/stderr directly instead) and pass it along to the next pipe 
| #pipe stdout to 
logger -it "my_script" 

所以这取决于你想要做什么之后,每个命令的结果裂开除了你的命令(在上面的FOO被重定向到标准错误,并没有被输送到tee

如果你要打印FOO到标准错误,并通过标准输出到你的脚本ÿ OU可以做

echo "foo" | tee /dev/stderr | yourscirpt 

然后tee将打印到stderr和foo将得到管道的标准输出到yourscript。

+0

你完全赞同我的意图 - 打印到当前的stderr,然后在其他地方输送。 – ChaimKut

相关问题