When one of my programs returns with a non-zero exit code, I want to avoid redirecting its output. Is this possible, and, if so, how do I do this?
My failed attempt:
echo foo > file
false | cat > file
this results in file
being empty. The behaviour I want is to only adjust file
when the program succeeds.
I also wonder whether it is possible do only update a file if the output is non-empty, without using multiple files.
You can use it like this:
out=$(some_command) && echo "$out" > outfile
echo "$out" > outfile
will execute only when some_command
succeeds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With