使用 Bash 重定向到多个文件时重定向不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4505339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:08:51  来源:igfitidea点击:

Ambiguous redirect when redirecting to multiple files using Bash

bashredirectpipe

提问by Synesso

$ echo "" >  /home/jem/rep_0[1-3]/logs/SystemOut.log
bash: /home/jem/rep_0[1-3]/logs/SystemOut.log: ambiguous redirect

Can I redirect to multiple files at a time?

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

Edit: Any answer that allows use of the ambiguous file reference?

编辑:任何允许使用不明确文件引用的答案?

回答by Laurence Gonsalves

That's what tee is for:

这就是 T 恤的用途:

command | tee file1 file2 file3 > file4

tee also outputs to stdout, so you may want either to put one file after a redirect (as shown above), or send stdout to /dev/null.

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

For your case:

对于您的情况:

echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log >/dev/null

回答by moinudin

You can do this using tee, which reads from stdin and writes to stdout and files. Since teealso outputs to stdout, I've chosen to direct it's output to /dev/null. Note that bash expansion matches against the existing files, so the files you're trying to write to must exist before executing this command for it to work.

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

$ echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log > /dev/null

As a side note, the ""you pass to echois redundant.

作为旁注,""您传递给的echo是多余的。

Not directly relevant to your question, but if you don't rely on bash expansion you can have multiple pipes.

与您的问题没有直接关系,但如果您不依赖 bash 扩展,您可以拥有多个管道。

$ echo hello > foo > bar > baz
$ cat foo bar baz
hello
hello
hello

回答by kidbrax

I had this same question and just wanted to add the example with the wildcard since it hadn't been shown. I think this is what you were looking for:

我有同样的问题,只是想添加带有通配符的示例,因为它尚未显示。我想这就是你要找的:

echo "" | tee *.log

回答by Paused until further notice.

You can do this:

你可以这样做:

echo "" | tee /home/jem/rep_0{1..3}/logs/SystemOut.log

To suppress the output to stdout, add this to the end of the commands above:

要禁止输出到标准输出,请将其添加到上述命令的末尾:

> /dev/null

The echocommand in your question (which doesn't require the empty quotes) simply puts a newline in the files. If you want to create empty files, use the touchcommand.

echo您问题中的命令(不需要空引号)只是在文件中放置一个换行符。如果要创建空文件,请使用该touch命令。

回答by Robert

No. What about using teetwice?

不。使用tee两次怎么样?

echo "Your text" | tee file1 | tee file2 > file3

回答by tletnes

Pipe to the "tee" command to branch to a file and std out, the cascade the tee commands

管道到“tee”命令以分支到文件和标准输出,级联 tee 命令