This is more of a architectural question:
I have a script that does some in file sed replacements/additions like:
sed -i 's/MY_VAR = 1000/MY_VAR = 1000\nMY_VAR2 = 500/' file.txt
which works fine but this is part of a fairly large script and someone may either
^C the first run and re-run this or just re-run it N-times back-to-back which will result in something like
MY_VAR = 1000 MY_VAR2 = 500 MY_VAR2 = 500 MY_VAR2 = 500 MY_VAR2 = 500 ...
which might not be expected. So my question is: what's the best way to avoid this? I came up with something like:
if [ ! -f file.txt~copy ] then cp file.txt file.txt~copy sed -i 's/MY_VAR = 1000/MY_VAR = 1000\nMY_VAR2 = 500/' file.txt fi
which should work fine but I was wondering if there's a better/recommended way to go about it? The above could obviously be problematic if your files are significantly large and you may just want to
touch a copy file for protection instead.