How to make appends to files fail if the file does not exist already?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












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






share|improve this question






















  • I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
    – Hakan Baba
    Nov 19 '17 at 6:25














up vote
2
down vote

favorite












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






share|improve this question






















  • I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
    – Hakan Baba
    Nov 19 '17 at 6:25












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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






share|improve this question














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








share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '17 at 6:22

























asked Nov 18 '17 at 22:20









Hakan Baba

244215




244215











  • I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
    – Hakan Baba
    Nov 19 '17 at 6:25
















  • I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
    – Hakan Baba
    Nov 19 '17 at 6:25















I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
– Hakan Baba
Nov 19 '17 at 6:25




I do not think I can achieve this with >> (output-redirection). According to this >> operation opens files with O_CREAT.
– Hakan Baba
Nov 19 '17 at 6:25










1 Answer
1






active

oldest

votes

















up vote
1
down vote













I think this should accomplish what you want:



sed -i '' -e '$a
Something
' SomeFile


I did this with mac osx/bsd sed so you may be able to remove the -i '' part. This will fail if SomeFile does not exist however a caveat to this is it will also fail if the file exists and is 0 bytes, hopefully that isn't a deal breaker for you.






share|improve this answer




















  • I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
    – Hakan Baba
    Nov 19 '17 at 6:17











  • My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
    – Hakan Baba
    Nov 19 '17 at 6:19










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405497%2fhow-to-make-appends-to-files-fail-if-the-file-does-not-exist-already%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













I think this should accomplish what you want:



sed -i '' -e '$a
Something
' SomeFile


I did this with mac osx/bsd sed so you may be able to remove the -i '' part. This will fail if SomeFile does not exist however a caveat to this is it will also fail if the file exists and is 0 bytes, hopefully that isn't a deal breaker for you.






share|improve this answer




















  • I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
    – Hakan Baba
    Nov 19 '17 at 6:17











  • My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
    – Hakan Baba
    Nov 19 '17 at 6:19














up vote
1
down vote













I think this should accomplish what you want:



sed -i '' -e '$a
Something
' SomeFile


I did this with mac osx/bsd sed so you may be able to remove the -i '' part. This will fail if SomeFile does not exist however a caveat to this is it will also fail if the file exists and is 0 bytes, hopefully that isn't a deal breaker for you.






share|improve this answer




















  • I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
    – Hakan Baba
    Nov 19 '17 at 6:17











  • My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
    – Hakan Baba
    Nov 19 '17 at 6:19












up vote
1
down vote










up vote
1
down vote









I think this should accomplish what you want:



sed -i '' -e '$a
Something
' SomeFile


I did this with mac osx/bsd sed so you may be able to remove the -i '' part. This will fail if SomeFile does not exist however a caveat to this is it will also fail if the file exists and is 0 bytes, hopefully that isn't a deal breaker for you.






share|improve this answer












I think this should accomplish what you want:



sed -i '' -e '$a
Something
' SomeFile


I did this with mac osx/bsd sed so you may be able to remove the -i '' part. This will fail if SomeFile does not exist however a caveat to this is it will also fail if the file exists and is 0 bytes, hopefully that isn't a deal breaker for you.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '17 at 22:58









Jesse_b

10.5k22659




10.5k22659











  • I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
    – Hakan Baba
    Nov 19 '17 at 6:17











  • My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
    – Hakan Baba
    Nov 19 '17 at 6:19
















  • I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
    – Hakan Baba
    Nov 19 '17 at 6:17











  • My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
    – Hakan Baba
    Nov 19 '17 at 6:19















I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
– Hakan Baba
Nov 19 '17 at 6:17





I have not used sed much except for simple replace s/../../g. So sorry for uneducated questions. For gnu-sed I needed to remove the '' but not the -i. But in that case the gnu-sed appends one more extra line after Something Do you need the final new line in the sed script? I think I can work around the 0 bytes problem. I can make the file contain a new line at the start instead of being empty. Why is this command a NOOP for empty files? Is it because sed is line based and skips processing if it cannot find any lines ?
– Hakan Baba
Nov 19 '17 at 6:17













My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
– Hakan Baba
Nov 19 '17 at 6:19




My issue with this solution is portability. I need the solution to work in BSD and in Linux. I will update the question accordingly. It should not be too difficult to modify the parameters and the sed script to generate the same output for bsd and gnu seds.
– Hakan Baba
Nov 19 '17 at 6:19

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405497%2fhow-to-make-appends-to-files-fail-if-the-file-does-not-exist-already%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay