bash 如何将多个命令的输出重定向到一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20355264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to redirect output of multiple commands to one file
提问by user2864207
i have a bash script, that has the following two commands:
我有一个 bash 脚本,它有以下两个命令:
ssh host tail -f /some/file | awk ..... > /some/file &
ssh host tail -f /some/file | grep .... > /some/file &
How can i make the output of both commands be directed into the same file.
如何将两个命令的输出定向到同一个文件中。
采纳答案by glenn Hymanman
Note you can reduce the number of ssh calls:
请注意,您可以减少 ssh 调用的次数:
{ ssh host tail -f /some/file |
tee >(awk ...) >(grep ...) >/dev/null
} > /some/file &
example:
例子:
{ echo foobar | tee >(sed 's/foo/FOO/') >(sed 's/bar/BAR/') > /dev/null; } > outputfile
cat outputfile
fooBAR
FOObar
回答by Jonathan Leffler
Either use 'append' with >>
or use braces to encompass the I/O redirections, or (occasionally) use exec
:
使用带有>>
或使用大括号的“附加”来包含 I/O 重定向,或者(偶尔)使用exec
:
ssh host tail -f /some/file | awk ..... > /some/file &
ssh host tail -f /some/file | grep .... >> /some/file &
or:
或者:
{
ssh host tail -f /some/file | awk ..... &
ssh host tail -f /some/file | grep .... &
} > /some/file
or:
或者:
exec > /some/file
ssh host tail -f /some/file | awk ..... &
ssh host tail -f /some/file | grep .... &
After the exec
, the standard output of the script as a whole goes to /some/file
. I seldom use this technique; I usually use the { ...; }
technique instead.
之后exec
,整个脚本的标准输出转到/some/file
. 我很少使用这种技术;我通常使用这种{ ...; }
技术。
Note: You do have to be careful with the braces notation. What I showed will work. Trying to flatten it onto one line requires you to treat the {
as if it were a command (followed by a space, for example) and also to treat the }
as if it were a command. You must have a command terminator before the }
— I used a newline, but an &
for background or ;
would work too.
注意:您必须小心大括号表示法。我展示的东西会起作用。试图将其展平到一行需要您将 the{
视为命令(例如,后跟一个空格)并将 the}
视为命令。您必须在}
- 我使用换行符之前有一个命令终止符,但&
用于背景或;
也可以使用。
Thus:
因此:
{ command1; command2; } >/some/file
{ command1 & command2 & } >/some/file
I also have not addressed the issue of why you have two separate tail -f
operations running on a single remote file and why you are not using awk
power as a super-grep
to handle it all in one — I've only addressed the surface question of how to redirect the I/O of the two commands to one file.
我也没有解决为什么tail -f
在一个远程文件上运行两个单独的操作以及为什么不使用awk
power 作为一个超级grep
来处理这一切的问题——我只解决了如何重定向的表面问题两个命令对一个文件的 I/O。
回答by twalberg
The best answer to this is to probably get rid of the ssh .... | grep ...
line, and modify the awk
script in the other command to add the functionality you were getting from the grep
command...
对此的最佳答案是可能摆脱该ssh .... | grep ...
行,并修改awk
另一个命令中的脚本以添加您从grep
命令中获得的功能...
That will get rid of any interleaving issues as a bonus side-effect.
这将消除任何交错问题作为额外的副作用。