How to make appends to files fail if the file does not exist already?
Clash 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
bash shell-script io-redirection gnu bsd
add a comment |Â
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
bash shell-script io-redirection gnu bsd
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
add a comment |Â
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
bash shell-script io-redirection gnu bsd
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
bash shell-script io-redirection gnu bsd
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
add a comment |Â
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
add a comment |Â
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.
I have not used sed much except for simple replaces/../../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 afterSomething
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
add a comment |Â
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.
I have not used sed much except for simple replaces/../../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 afterSomething
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
add a comment |Â
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.
I have not used sed much except for simple replaces/../../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 afterSomething
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
add a comment |Â
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.
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.
answered Nov 18 '17 at 22:58
Jesse_b
10.5k22659
10.5k22659
I have not used sed much except for simple replaces/../../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 afterSomething
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
add a comment |Â
I have not used sed much except for simple replaces/../../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 afterSomething
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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