How can I output a command to a file, without getting a blank file on error?
I'm trying to run a command, write that to a file, and then I'm using that file for something else.
The gist of what I need is:
myAPICommand.exe parameters > myFile.txt
The problem is that myAPICommand.exe
fails a lot. I attempt to fix some of the problems and rerun, but I get hit with "cannot overwrite existing file". I have to run a separate rm
command to cleanup the blank myFile.txt
and then rerun myAPICommand.exe
.
It's not the most egregious problem, but it is annoying.
How can I avoid writing a blank file when my base command fails?
linux command-line output error-handling write
add a comment |
I'm trying to run a command, write that to a file, and then I'm using that file for something else.
The gist of what I need is:
myAPICommand.exe parameters > myFile.txt
The problem is that myAPICommand.exe
fails a lot. I attempt to fix some of the problems and rerun, but I get hit with "cannot overwrite existing file". I have to run a separate rm
command to cleanup the blank myFile.txt
and then rerun myAPICommand.exe
.
It's not the most egregious problem, but it is annoying.
How can I avoid writing a blank file when my base command fails?
linux command-line output error-handling write
1
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
4
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.
– mosvy
Dec 18 at 0:53
add a comment |
I'm trying to run a command, write that to a file, and then I'm using that file for something else.
The gist of what I need is:
myAPICommand.exe parameters > myFile.txt
The problem is that myAPICommand.exe
fails a lot. I attempt to fix some of the problems and rerun, but I get hit with "cannot overwrite existing file". I have to run a separate rm
command to cleanup the blank myFile.txt
and then rerun myAPICommand.exe
.
It's not the most egregious problem, but it is annoying.
How can I avoid writing a blank file when my base command fails?
linux command-line output error-handling write
I'm trying to run a command, write that to a file, and then I'm using that file for something else.
The gist of what I need is:
myAPICommand.exe parameters > myFile.txt
The problem is that myAPICommand.exe
fails a lot. I attempt to fix some of the problems and rerun, but I get hit with "cannot overwrite existing file". I have to run a separate rm
command to cleanup the blank myFile.txt
and then rerun myAPICommand.exe
.
It's not the most egregious problem, but it is annoying.
How can I avoid writing a blank file when my base command fails?
linux command-line output error-handling write
linux command-line output error-handling write
edited Dec 17 at 14:41
asked Dec 17 at 14:37
goodguy5
1356
1356
1
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
4
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.
– mosvy
Dec 18 at 0:53
add a comment |
1
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
4
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.
– mosvy
Dec 18 at 0:53
1
1
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
4
4
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.
t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.– mosvy
Dec 18 at 0:53
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.
t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.– mosvy
Dec 18 at 0:53
add a comment |
3 Answers
3
active
oldest
votes
You must have "noclobber" set, check the following example:
$ echo 1 > 1 # create file
$ cat 1
1
$ echo 2 > 1 # overwrite file
$ cat 1
2
$ set -o noclobber
$ echo 3 > 1 # file is now protected from accidental overwrite
bash: 1: cannot overwrite existing file
$ cat 1
2
$ echo 3 >| 1 # temporary allow overwrite
$ cat 1
3
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ cat 1
3
$ set +o noclobber
$ echo 4 > 1
$ cat 1
4
"noclobber" is only for overwrite, you can still append though:
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ echo 4 >> 1
To check if you have that flag set you can type echo $-
and see if you have C
flag set (or set -o |grep clobber
).
Q: How can I avoid writing a blank file when my base command fails?
Any requirements? You could just simply store the output in a variable and then check if it is empty. Check the following example (note that the way you check the variable needs fine adjusting to your needs, in the example I didn't quote it or use anything like ${cmd_output+x}
which checks if variable is set, to avoid writing a file containing whitespaces only.
$ cmd_output=$(echo)
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'nnn')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e ' ')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'something')
$ test $cmd_output && echo yes || echo no
yes
$ cmd_output=$(myAPICommand.exe parameters)
$ test $cmd_output && echo "$cmd_output" > myFile.txt
Example without using a single variable holding the whole output:
log() { while read data; do echo "$data" >> myFile.txt; done; }
myAPICommand.exe parameters |log
New contributor
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
I think you need to quote$cmd_output
otherwise you get unexpected results if$cmd_output
is (say)1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it intogrep -q
with a suitable pattern.
– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
|
show 2 more comments
You can delete the file after running, if the command fails, with
myAPICommand parameters > myFile.txt || rm myFile.txt
But I would suggest clobbering the file instead:
myAPICommand parameters >| myFile.txt
See What are the shell's control and redirection operators? for details.
add a comment |
You could create a script to run the myAPICommand.exe, but have it first remove the myFile.txt if it exists. Then you don't have to constantly do the rm command to clean up.
Like:
if [ -e myFile.txt ]
then
rm myFile.txt && myAPICommand.exe
else
You could also make it so that your command cleans up after itself. If the file is empty adding something like the following.
Like:
if [ -s myFile.txt ]
then
EXIT 0
else
rm myFile.txt && EXIT 1
fi
1
Or justrm -f
it, which won't complain if the file doesn't exist.
– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489489%2fhow-can-i-output-a-command-to-a-file-without-getting-a-blank-file-on-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You must have "noclobber" set, check the following example:
$ echo 1 > 1 # create file
$ cat 1
1
$ echo 2 > 1 # overwrite file
$ cat 1
2
$ set -o noclobber
$ echo 3 > 1 # file is now protected from accidental overwrite
bash: 1: cannot overwrite existing file
$ cat 1
2
$ echo 3 >| 1 # temporary allow overwrite
$ cat 1
3
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ cat 1
3
$ set +o noclobber
$ echo 4 > 1
$ cat 1
4
"noclobber" is only for overwrite, you can still append though:
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ echo 4 >> 1
To check if you have that flag set you can type echo $-
and see if you have C
flag set (or set -o |grep clobber
).
Q: How can I avoid writing a blank file when my base command fails?
Any requirements? You could just simply store the output in a variable and then check if it is empty. Check the following example (note that the way you check the variable needs fine adjusting to your needs, in the example I didn't quote it or use anything like ${cmd_output+x}
which checks if variable is set, to avoid writing a file containing whitespaces only.
$ cmd_output=$(echo)
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'nnn')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e ' ')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'something')
$ test $cmd_output && echo yes || echo no
yes
$ cmd_output=$(myAPICommand.exe parameters)
$ test $cmd_output && echo "$cmd_output" > myFile.txt
Example without using a single variable holding the whole output:
log() { while read data; do echo "$data" >> myFile.txt; done; }
myAPICommand.exe parameters |log
New contributor
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
I think you need to quote$cmd_output
otherwise you get unexpected results if$cmd_output
is (say)1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it intogrep -q
with a suitable pattern.
– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
|
show 2 more comments
You must have "noclobber" set, check the following example:
$ echo 1 > 1 # create file
$ cat 1
1
$ echo 2 > 1 # overwrite file
$ cat 1
2
$ set -o noclobber
$ echo 3 > 1 # file is now protected from accidental overwrite
bash: 1: cannot overwrite existing file
$ cat 1
2
$ echo 3 >| 1 # temporary allow overwrite
$ cat 1
3
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ cat 1
3
$ set +o noclobber
$ echo 4 > 1
$ cat 1
4
"noclobber" is only for overwrite, you can still append though:
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ echo 4 >> 1
To check if you have that flag set you can type echo $-
and see if you have C
flag set (or set -o |grep clobber
).
Q: How can I avoid writing a blank file when my base command fails?
Any requirements? You could just simply store the output in a variable and then check if it is empty. Check the following example (note that the way you check the variable needs fine adjusting to your needs, in the example I didn't quote it or use anything like ${cmd_output+x}
which checks if variable is set, to avoid writing a file containing whitespaces only.
$ cmd_output=$(echo)
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'nnn')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e ' ')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'something')
$ test $cmd_output && echo yes || echo no
yes
$ cmd_output=$(myAPICommand.exe parameters)
$ test $cmd_output && echo "$cmd_output" > myFile.txt
Example without using a single variable holding the whole output:
log() { while read data; do echo "$data" >> myFile.txt; done; }
myAPICommand.exe parameters |log
New contributor
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
I think you need to quote$cmd_output
otherwise you get unexpected results if$cmd_output
is (say)1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it intogrep -q
with a suitable pattern.
– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
|
show 2 more comments
You must have "noclobber" set, check the following example:
$ echo 1 > 1 # create file
$ cat 1
1
$ echo 2 > 1 # overwrite file
$ cat 1
2
$ set -o noclobber
$ echo 3 > 1 # file is now protected from accidental overwrite
bash: 1: cannot overwrite existing file
$ cat 1
2
$ echo 3 >| 1 # temporary allow overwrite
$ cat 1
3
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ cat 1
3
$ set +o noclobber
$ echo 4 > 1
$ cat 1
4
"noclobber" is only for overwrite, you can still append though:
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ echo 4 >> 1
To check if you have that flag set you can type echo $-
and see if you have C
flag set (or set -o |grep clobber
).
Q: How can I avoid writing a blank file when my base command fails?
Any requirements? You could just simply store the output in a variable and then check if it is empty. Check the following example (note that the way you check the variable needs fine adjusting to your needs, in the example I didn't quote it or use anything like ${cmd_output+x}
which checks if variable is set, to avoid writing a file containing whitespaces only.
$ cmd_output=$(echo)
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'nnn')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e ' ')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'something')
$ test $cmd_output && echo yes || echo no
yes
$ cmd_output=$(myAPICommand.exe parameters)
$ test $cmd_output && echo "$cmd_output" > myFile.txt
Example without using a single variable holding the whole output:
log() { while read data; do echo "$data" >> myFile.txt; done; }
myAPICommand.exe parameters |log
New contributor
You must have "noclobber" set, check the following example:
$ echo 1 > 1 # create file
$ cat 1
1
$ echo 2 > 1 # overwrite file
$ cat 1
2
$ set -o noclobber
$ echo 3 > 1 # file is now protected from accidental overwrite
bash: 1: cannot overwrite existing file
$ cat 1
2
$ echo 3 >| 1 # temporary allow overwrite
$ cat 1
3
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ cat 1
3
$ set +o noclobber
$ echo 4 > 1
$ cat 1
4
"noclobber" is only for overwrite, you can still append though:
$ echo 4 > 1
bash: 1: cannot overwrite existing file
$ echo 4 >> 1
To check if you have that flag set you can type echo $-
and see if you have C
flag set (or set -o |grep clobber
).
Q: How can I avoid writing a blank file when my base command fails?
Any requirements? You could just simply store the output in a variable and then check if it is empty. Check the following example (note that the way you check the variable needs fine adjusting to your needs, in the example I didn't quote it or use anything like ${cmd_output+x}
which checks if variable is set, to avoid writing a file containing whitespaces only.
$ cmd_output=$(echo)
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'nnn')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e ' ')
$ test $cmd_output && echo yes || echo no
no
$ cmd_output=$(echo -e 'something')
$ test $cmd_output && echo yes || echo no
yes
$ cmd_output=$(myAPICommand.exe parameters)
$ test $cmd_output && echo "$cmd_output" > myFile.txt
Example without using a single variable holding the whole output:
log() { while read data; do echo "$data" >> myFile.txt; done; }
myAPICommand.exe parameters |log
New contributor
edited Dec 18 at 3:34
New contributor
answered Dec 17 at 14:52
Evolter
1364
1364
New contributor
New contributor
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
I think you need to quote$cmd_output
otherwise you get unexpected results if$cmd_output
is (say)1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it intogrep -q
with a suitable pattern.
– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
|
show 2 more comments
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
I think you need to quote$cmd_output
otherwise you get unexpected results if$cmd_output
is (say)1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it intogrep -q
with a suitable pattern.
– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
1
1
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I like this answer and will likely accept it eventually. While it fixes my problem, it doesn't actually answer the question.
– goodguy5
Dec 17 at 14:59
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
I have updated my answer if that helps.
– Evolter
Dec 17 at 15:52
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
lovely. Thank you. I didn't think of shortcircuit logic.
– goodguy5
Dec 17 at 16:10
2
2
I think you need to quote
$cmd_output
otherwise you get unexpected results if $cmd_output
is (say) 1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it into grep -q
with a suitable pattern.– Ben Millwood
Dec 17 at 16:35
I think you need to quote
$cmd_output
otherwise you get unexpected results if $cmd_output
is (say) 1 == 2
. If you want to protect against "$cmd_output" being only whitespace, I would consider piping it into grep -q
with a suitable pattern.– Ben Millwood
Dec 17 at 16:35
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
I agree that it isn't bulletproof example @BenMillwood it just suppose to give an idea. With more requirements (different shells handle variables a bit differently, etc.) we could make it better :)
– Evolter
Dec 17 at 16:50
|
show 2 more comments
You can delete the file after running, if the command fails, with
myAPICommand parameters > myFile.txt || rm myFile.txt
But I would suggest clobbering the file instead:
myAPICommand parameters >| myFile.txt
See What are the shell's control and redirection operators? for details.
add a comment |
You can delete the file after running, if the command fails, with
myAPICommand parameters > myFile.txt || rm myFile.txt
But I would suggest clobbering the file instead:
myAPICommand parameters >| myFile.txt
See What are the shell's control and redirection operators? for details.
add a comment |
You can delete the file after running, if the command fails, with
myAPICommand parameters > myFile.txt || rm myFile.txt
But I would suggest clobbering the file instead:
myAPICommand parameters >| myFile.txt
See What are the shell's control and redirection operators? for details.
You can delete the file after running, if the command fails, with
myAPICommand parameters > myFile.txt || rm myFile.txt
But I would suggest clobbering the file instead:
myAPICommand parameters >| myFile.txt
See What are the shell's control and redirection operators? for details.
answered Dec 17 at 14:51
Stephen Kitt
163k24362441
163k24362441
add a comment |
add a comment |
You could create a script to run the myAPICommand.exe, but have it first remove the myFile.txt if it exists. Then you don't have to constantly do the rm command to clean up.
Like:
if [ -e myFile.txt ]
then
rm myFile.txt && myAPICommand.exe
else
You could also make it so that your command cleans up after itself. If the file is empty adding something like the following.
Like:
if [ -s myFile.txt ]
then
EXIT 0
else
rm myFile.txt && EXIT 1
fi
1
Or justrm -f
it, which won't complain if the file doesn't exist.
– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
add a comment |
You could create a script to run the myAPICommand.exe, but have it first remove the myFile.txt if it exists. Then you don't have to constantly do the rm command to clean up.
Like:
if [ -e myFile.txt ]
then
rm myFile.txt && myAPICommand.exe
else
You could also make it so that your command cleans up after itself. If the file is empty adding something like the following.
Like:
if [ -s myFile.txt ]
then
EXIT 0
else
rm myFile.txt && EXIT 1
fi
1
Or justrm -f
it, which won't complain if the file doesn't exist.
– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
add a comment |
You could create a script to run the myAPICommand.exe, but have it first remove the myFile.txt if it exists. Then you don't have to constantly do the rm command to clean up.
Like:
if [ -e myFile.txt ]
then
rm myFile.txt && myAPICommand.exe
else
You could also make it so that your command cleans up after itself. If the file is empty adding something like the following.
Like:
if [ -s myFile.txt ]
then
EXIT 0
else
rm myFile.txt && EXIT 1
fi
You could create a script to run the myAPICommand.exe, but have it first remove the myFile.txt if it exists. Then you don't have to constantly do the rm command to clean up.
Like:
if [ -e myFile.txt ]
then
rm myFile.txt && myAPICommand.exe
else
You could also make it so that your command cleans up after itself. If the file is empty adding something like the following.
Like:
if [ -s myFile.txt ]
then
EXIT 0
else
rm myFile.txt && EXIT 1
fi
edited 16 hours ago
Jeff Schaller
38.5k1053125
38.5k1053125
answered Dec 17 at 16:31
Michael Prokopec
1,026116
1,026116
1
Or justrm -f
it, which won't complain if the file doesn't exist.
– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
add a comment |
1
Or justrm -f
it, which won't complain if the file doesn't exist.
– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
1
1
Or just
rm -f
it, which won't complain if the file doesn't exist.– Ben Millwood
Dec 17 at 16:36
Or just
rm -f
it, which won't complain if the file doesn't exist.– Ben Millwood
Dec 17 at 16:36
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
True.. @BenMillwood
– Michael Prokopec
Dec 17 at 16:37
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489489%2fhow-can-i-output-a-command-to-a-file-without-getting-a-blank-file-on-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How is failure indicated? A return code? An empty file?
– Jeff Schaller
Dec 17 at 14:52
@JeffSchaller error is written to the command line, in this case.
– goodguy5
Dec 17 at 18:33
"command-line" meaning redirected to myFile.txt as "stdout", or to the screen in this case, as "stderr"?
– Jeff Schaller
Dec 17 at 18:34
@JeffSchaller, I'm sorry. stderr
– goodguy5
Dec 17 at 18:57
4
Sorry, but you're having it completely backwards; you should redirect the output to a temporary file, and rename the temp file to the final destination if your command succeeds.
t=$(mktemp); trap 'rm -f "$t"' EXIT INT TERM; the_cmd >"$t" && mv "$t" the_file
. That way, the output file will always contain valid data irrespective on whether the command succeeded or failed.– mosvy
Dec 18 at 0:53