When doing:
CMD > file.out
if CMD fails (non-zero exit code), 'file.out' with be empty or incomplete.
I looking for a way to cleanup in case the CMD fails.
When CMD is run in a CRON job I would prefer for the directed output file to be deleted instead of leaving an empty/incomplete file behind.
My initial idea was:
if ! CMD > file.out; then rm file.out; fi
However it hides the CMD exit status, making it unsuitable. (especially for CRON)
- Can anyone think a better solution ?
- Is there some functionality in bash or sh that would allow this ?
- I was hoping maybe there is some 'tee' like utility that could:
- It would redirect STDOUT output to a file
- If the command fails it would remove output file
- In all case it would return the CMD exit code