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

发布于 2024-10-08 17:30:47 字数 200 浏览 5 评论 0原文

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

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

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

$ 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

痴情换悲伤 2024-10-15 17:30:47

这就是 tee 的用途:

command | tee file1 file2 file3 > file4

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

对于你的情况:

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

That's what tee is for:

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.

For your case:

echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log >/dev/null
寄意 2024-10-15 17:30:47

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

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

附带说明一下,您传递给 echo"" 是多余的。

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

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

You can do this using tee, which reads from stdin and writes to stdout and files. Since tee also 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.

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

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

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

$ echo hello > foo > bar > baz
$ cat foo bar baz
hello
hello
hello
空城之時有危險 2024-10-15 17:30:47

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

echo "" | tee *.log

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
二货你真萌 2024-10-15 17:30:47

您可以这样做:

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

要禁止输出到 stdout,请将其添加到上面命令的末尾:

> /dev/null

问题中的 echo 命令(不需要空引号)只需在文件。如果您想创建空文件,请使用 touch 命令。

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 echo command 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 touch command.

夜未央樱花落 2024-10-15 17:30:47

不。使用 tee 两次怎么样?

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

No. What about using tee twice?

echo "Your text" | tee file1 | tee file2 > file3
且行且努力 2024-10-15 17:30:47

管道到“tee”命令以分支到文件并 std 输出,级联 tee 命令

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文