#!/bin/bash

> /tmp/foo
exec 1>/tmp/foo

echo a
echo B>>/tmp/foo
echo c
echo D>>/tmp/foo
echo e
echo F>>/tmp/foo

That script creates two simultaneous writers of /tmp/foo
(one via the "exec >", another via each "echo >>")
but does not provide any concurrency control.
Shame on the script; the results are undefined.
The only possible legitimate complaint _might_ be
that bash did not detect and warn about the user error.

The fix is to provide some concurrency control,
or not to create multiple simultaneous writers of the same file.
Because all output is going to the same file, then the simplest
solution is to omit all the appending ">>/tmp/foo" because
they are subsumed by the initial redirection "exec >/tmp/foo".

Perhaps concurrency control can be provided by using a fifo:

        mkfifo /tmp/my_fifo
        cp /tmp/my_fifo /tmp/foo &   # constantly drain the fifo

        exec >/tmp/my_fifo
        echo a
        echo B >>/tmp/my_fifo

etc.  However, this relies upon bash _not_ buffering
the first redirection, and that is not guaranteed.

Bottom line: avoid simultaneous writers of the same file.

--


Reply via email to