Ambiguous redirect when redirecting to multiple fi

2020-03-12 11:21发布

问题:

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

回答1:

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


回答2:

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


回答3:

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


回答4:

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.



回答5:

No. What about using tee twice?

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


回答6:

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