Bash模糊重定向 - 重定向到多个文件

Syn*_*sso 9 bash redirect pipe

$ echo "" >  /home/jem/rep_0[1-3]/logs/SystemOut.log
bash: /home/jem/rep_0[1-3]/logs/SystemOut.log: ambiguous redirect
Run Code Online (Sandbox Code Playgroud)

我可以一次重定向到多个文件吗?

编辑:任何允许使用模糊文件引用的答案?

Lau*_*ves 21

这就是发球台的用途:

command | tee file1 file2 file3 > file4
Run Code Online (Sandbox Code Playgroud)

tee也输出到stdout,因此你可能想要在重定向之后放置一个文件(如上所示),或者发送stdout /dev/null.

对于你的情况:

echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log >/dev/null
Run Code Online (Sandbox Code Playgroud)


mar*_*cog 5

您可以使用tee,从stdin读取并写入stdout和文件.既然tee输出到stdout,我也选择将它的输出定向到/dev/null.请注意,bash扩展与现有文件匹配,因此在执行此命令之前,您尝试写入的文件必须存在才能生效.

$ echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log > /dev/null
Run Code Online (Sandbox Code Playgroud)

作为旁注,""你传递的echo是多余的.

与您的问题没有直接关系,但如果您不依赖于bash扩展,则可以使用多个管道.

$ echo hello > foo > bar > baz
$ cat foo bar baz
hello
hello
hello
Run Code Online (Sandbox Code Playgroud)