bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mixing exec redirection with explicit redirection, unexpected result


From: John Reiser
Subject: Re: Mixing exec redirection with explicit redirection, unexpected results
Date: Tue, 08 Dec 2009 18:58:02 -0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4pre) Gecko/20091014 Fedora/3.0-2.8.b4.fc11 Thunderbird/3.0b4

#!/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

[Prev in Thread] Current Thread [Next in Thread]