For example
echo Something >> SomeFile
I want this append to work only if SomeFile
exists already.
Right now I am using the following:
if [ -e SomeFile ]; then
echo Something >> SomeFile
fi;
But there should be a race condition here. During the if condition evaluation SomeFile
may exist. A context switch happens between the if condition and the append. Some other application executes that removes SomeFile
. In that case the append would create SomeFile
.
I need to solution to work for both bsd sed and gnu sed.
It is pretty simple to do this in python with os.open and O_APPEND
$ rm SomeFile
$ python -c "import os; print(os.open(\"SomeFile\",os.O_APPEND))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
OSError: [Errno 2] No such file or directory: 'SomeFile'
$ touch SomeFile
$ python -c "import os; print(os.open(\"SomeFile\",os.O_APPEND))"
3
>>
(output-redirection). According to this>>
operation opens files with O_CREAT.