The difference between redirects

Is there a difference between the two lines?

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

Because Robert told me that the first one does only I / O OP outside the for loop.

But if I type tail -f /tmp/output.txt, it behaves exactly the same.

+4
source share
2 answers

They do the same if they succeed. However, there may be noticeable differences if they fail for any reason.

The first:

for ...; do
   # things
done >> file

This will redirect to the file, presumably after the loop has completed. However, this can happen when Bash decides to flush the buffer.

Imagine that after iteration number 3, something does not work: you cannot say what was saved in the file.

:

for ...; do
   # things >> file
done

.

, 3 - : , .

:

(, puts, printf stdio.h C, cout << … ++, print ), : , ; , ( "" ), ( ). , .

, , :

, 500000 ( ).

: , , Bash , . , .

+3

, : >> . .

, /tmp/output.txt , echo ... >> /tmp/output.txt , for ... done >> /tmp/output.txt .

, ( , , , Bash script ).

0

Source: https://habr.com/ru/post/1621964/


All Articles