重定向bash输出用于循环

时间:2022-09-18 20:22:03

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 2 is 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

我期待标准出来,被重定向出去。dat就像简单地运行命令seq 2 > out.dat一样

3 个解决方案

#1


62  

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 words that may cause pathname expansion.

另外,要注意可能导致路径名扩展的输出。

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中使用比backticks(')更清晰的命令替换语法,并且它支持嵌套。

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

#2


6  

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 echo statements, all of which shall go to the file, doing done > out.dat is probably more readable and easier to maintain. The advantage of this solution, of course, is that it gives more flexibility.

哪一个更好当然取决于用例。一口气写完这个文件肯定比把它加一千遍要好。另外,如果循环包含多个echo语句,那么所有这些语句都将进入该文件,并执行>。dat可能更易于阅读和维护。当然,这种解决方案的优点是它提供了更多的灵活性。

#3


5  

Try:

试一试:

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

#1


62  

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 words that may cause pathname expansion.

另外,要注意可能导致路径名扩展的输出。

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中使用比backticks(')更清晰的命令替换语法,并且它支持嵌套。

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

#2


6  

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 echo statements, all of which shall go to the file, doing done > out.dat is probably more readable and easier to maintain. The advantage of this solution, of course, is that it gives more flexibility.

哪一个更好当然取决于用例。一口气写完这个文件肯定比把它加一千遍要好。另外,如果循环包含多个echo语句,那么所有这些语句都将进入该文件,并执行>。dat可能更易于阅读和维护。当然,这种解决方案的优点是它提供了更多的灵活性。

#3


5  

Try:

试一试:

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