Like many others, I like to use temporary files. I use the shell process identifier as part of the temporary name, so if multiple instances of the script work at the same time, they will not conflict. Finally, I only overwrite the original file if the script succeeds (using a short circuit with the Boolean operator - it is a little tight, but very nice for simple command lines). Putting it all together, it will look like this:
some_script < file > smscrpt.$$ && mv smscrpt.$$ file
This will result in a temporary file if the command fails. If you want to clear the error, you can change it to:
some_script < file > smscrpt.$$ && mv smscrpt.$$ file || rm smscrpt.$$
By the way, I got rid of the catβs misuse and replaced it with input redirection.
R Samuel Klatchko
source share