I am trying to overwrite a file with command output, but only if there is any output. That is, I usually want
mycommand > myfile
but if this would overwrite
myfile with empty data, I wish to retain the old version of
myfile. I thought that something using
ifne should be possible, a la
mycommand | ifne (cat > myfile)
but that does not work ...
An indirect approach
mycommand | tee mytempfile | ifne mv mytempfile myfile
works, but I consider the use of that temp file unelegant.
Q: Why does my first idea not work? Can it be made work? Or is there another nice and perhaps completely different solution for my original problem?