Linux 重定向 bash for 循环的输出

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

Redirecting output of bash for loop

linuxbashshell

提问by Matthew Kirkley

I have a simple BASH command that looks like

我有一个简单的 BASH 命令,看起来像

for i in `seq 2`; do echo $i; done; > out.dat

When this runs the output of seq 2is output to the terminal and nothing is output to the data file (out.dat)

运行时,将输出seq 2输出到终端,并且没有任何内容输出到数据文件 (out.dat)

I am expecting standard out to be redirected to out.dat like it does simply running the command seq 2 > out.dat

我期待标准输出被重定向到 out.dat 就像它只是运行命令一样 seq 2 > out.dat

采纳答案by konsolebox

Remove your semicolon.

删除你的分号。

for i in `seq 2`; do echo "$i"; done > out.dat

SUGGESTIONS

建议

Also as suggested by Fredrik Pihl, try not to use external binaries when they are not needed, or at least when practically not:

同样按照 Fredrik Pihl 的建议,尽量不要在不需要时使用外部二进制文件,或者至少在实际上不需要时:

for i in {1..2}; do echo "$i"; done > out.dat
for ((i = 1; i <= 2; ++i )); do echo "$i"; done > out.dat
for i in 1 2; do echo "$i"; done > out.dat

Also, be careful of outputs in wordsthat may cause pathname expansion.

此外,请注意words可能导致路径名扩展的输出。

for A in $(echo '*'); do echo "$A"; done

Would show your files instead of just a literal *.

将显示您的文件,而不仅仅是文字*.

$()is also recommended as a clearer syntax for command substitution in Bash and POSIX shells than backticks (`), and it supports nesting.

$()在 Bash 和 POSIX shell 中,还推荐使用比反引号 ( `)更清晰的命令替换语法,并且它支持嵌套。

The cleaner solutions as well for reading output to variables are

将输出读取到变量的更干净的解决方案是

while read VAR; do
    ...   
done < <(do something)

And

read ... < <(do something)  ## Could be done on a loop or with readarray.

for A in "${ARRAY[@]}"; do
    :
done

Using printf can also be an easier alternative with respect to the intended function:

就预期功能而言,使用 printf 也可以是一种更简单的替代方法:

printf '%s\n' {1..2} > out.dat

回答by svante

Try:

尝试:

(for i in `seq 2`; do echo $i; done;) > out.dat

回答by tobias_k

Another possibility, for the sake of completeness: You can move the output inside the loop, using >>to append to the file, if it exists.

另一种可能性,为了完整起见:您可以将输出移动到循环内,使用>>附加到文件(如果存在)。

for i in `seq 2`; do echo $i >> out.dat; done;

Which one is better certainly depends on the use case. Writing the file in one go is certainly better than appending to it a thousand times. Also, if the loop contains multiple echostatements, all of which shall go to the file, doing done > out.datis probably more readable and easier to maintain. The advantage of this solution, of course, is that it gives more flexibility.

哪个更好当然取决于用例。一次写入文件肯定比附加一千次要好。此外,如果循环包含多个echo语句,所有这些语句都将写入文件,这样做done > out.dat可能更具可读性且更易于维护。当然,这种解决方案的优势在于它提供了更大的灵活性。