Linux 将循环输出到文件

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

Output for loop to a file

linuxbashshell

提问by EchelonSS

I am trying to have a for loop output to a text to a file 10 times. Here is what I have:

我正在尝试将 for 循环输出到文件的文本 10 次。这是我所拥有的:

for ((i=1;i<=10;i++)) ; do echo "Hello World" > testforloop.txt ;  done

This outputs "Hello World" once to the file "testforloop.txt". If I don't output to file it prints Hello World to the screen 10 times. Thanks for any help.

这将一次“Hello World”输出到文件“testforloop.txt”。如果我不输出到文件,它会将 Hello World 打印到屏幕 10 次。谢谢你的帮助。

采纳答案by torek

You are using >redirection, which wipes out the existing contents of the file and replaces it with the command's output, inside the loop. So this wipes out the previous contents 10 times, replacing it with one line each time.

您正在使用>重定向,它会在循环内清除文件的现有内容并将其替换为命令的输出。所以这会清除之前的内容 10 次,每次替换为一行。

Without the >, or with >/dev/tty, it goes to your display, where >cannot wipe anything out so you see all ten copies.

如果没有>>/dev/tty,它会进入您的显示器,在那里>无法擦除任何内容,因此您会看到所有十个副本。

You could use >>, which will still open the file ten times, but will append (not wipe out previous contents) each time. That's not terribly efficient though, and it retains data from a previous run (which may or may not be what you want).

您可以使用>>,它仍然会打开文件十次,但每次都会追加(不会删除以前的内容)。但这并不是非常有效,并且它保留了上次运行的数据(这可能是也可能不是您想要的)。

Or, you can redirect the entire loop once:

或者,您可以重定向整个循环一次:

for ... do cmd; done >file

which runs the entire loop with output redirected, creating (or, with >>, opening for append) only once.

它运行整个循环并重定向输出,仅创建(或使用>>,打开以进行追加)一次。

回答by rakib_

You're only redirecting the echo with ">", so it overwrites. What you need is to append using ">>" operator. Do the following:

您只是使用“>”重定向回声,因此它会覆盖。您需要的是使用“>>”运算符进行附加。请执行下列操作:

       for ((i=1;i<=10;i++)) ; do echo "Hello World" >> testforloop.txt ;  done

回答by Lando Calrissian

You rewrite the testforloop.txt ten times. If you did

您将 testforloop.txt 重写了十次。如果你做了

for ((i=1;i<=10;i++)) ; do echo "Hello World" > testforloop(i).txt ; done

对于 ((i=1;i<=10;i++)) ; 做 echo "Hello World" > testforloop(i).txt ; 完毕

where i is the int from the for loop. I'm not sure the language you're programming in.

其中 i 是 for 循环中的 int。我不确定您使用的编程语言。

回答by Raphael Silva

The following will solve your problem:

以下将解决您的问题:

You should try the >>operator for the reasons already mentioned with the command echo -e

>>由于命令中已经提到的原因,您应该尝试使用运算符echo -e

Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.

使用选项 '\n' - 带有退格解释器 '-e' 的新行从使用它的地方开始处理新行。

link

关联

Doing so:

这样做:

for ((i=1;i<=10;i++)) ; do echo -e "Hello World" >> testforloop.txt ; done

for ((i=1;i<=10;i++)) ; do echo -e "Hello World" >> testforloop.txt ; done

cat command:

猫命令:

cat testforloop.txt

cat testforloop.txt

output:

输出:

Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Alternatively, you can try:

或者,您可以尝试:

cat -n testforloop.txt

cat -n testforloop.txt

output:

输出:

1 Hello World 2 Hello World 3 Hello World 4 Hello World 5 Hello World 6 Hello World 7 Hello World 8 Hello World 9 Hello World 10 Hello World

1 Hello World 2 Hello World 3 Hello World 4 Hello World 5 Hello World 6 Hello World 7 Hello World 8 Hello World 9 Hello World 10 Hello World