这两行之间有什么区别吗?
for i in $(seq 1 10); do echo $i - `date`; sleep 1; done >> /tmp/output.txt
for i in $(seq 1 10); do echo $i - `date` >> /tmp/output.txt ; sleep 1; done
因为Robert told me表示第一个只在for循环外部产生I / O OP.
但是,如果我输入tail -f /tmp/output.txt,这行为方式完全相同.
解决方法:
如果他们成功,他们也会这样做.但是,如果因任何原因失败,可能会有明显的差异.
第一个:
for ...; do
# things
done >> file
这将在循环完成后重定向到文件.但是,只要Bash决定刷新缓冲区,就可能发生这种情况.
想象一下,在迭代次数3之后出现了一些问题:你无法分辨出文件中存储的内容.
第二个:
for ...; do
# things >> file
done
想象一下迭代次数3之后出现问题:您确定前两个循环已正确存储在文件中.
从How to redirect output from an infinite-loop program开始:
If your program is using the standard output functions (e.g.
puts
,
printf
and friends fromstdio.h
in C,cout << …
in C++,
in many high-level languages), then its output is
07001:
characters accumulate in a memory zone which is called a buffer; when
there’s too much data in the buffer, the contents of the buffer is
printed (it’s “flushed”) and the buffer becomes empty (ready to be
filled again). If your program doesn’t produce much output, it may not
have filled its buffer yet.
另外,从the answer you link开始:
Placing the redirection operator outside the loop doubles the
performance when writing 500000 lines (on my system).
这是有道理的:如果你必须在每个循环上刷新,那么只要它发现方便就让Bash刷新需要更多的时间.每次写一行比划线更容易.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。