Tuesday, May 14, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 594  / 2 Years ago, thu, january 20, 2022, 1:24:47

I wish to replace my failing memory with a very small shell script.



#!/bin/sh
if ! [ –a $1.sav ]; then
mv $1 $1.sav
cp $1.sav $1
fi
nano $1


is intended to save the original version of a script. If the original has been preserved before, it skips the move-and-copy-back (and I use move-and-copy-back to preserve the original timestamp).



This works as intended if, after I make it executable with chmod I launch it from within the directory where I am editing, e.g. with



./safe.sh filename



However, when I move it into /usr/bin (so it is in the path as verified with echo $PATH) and then I try to run it in a different directory (without the leading ./ ) it fails with



-bash: /usr/bin/safe.sh: /bin/sh: bad interpreter: Text file busy



D'oh? Inquiring minds want to know how to make this work.


More From » command-line

 Answers
4

Not an answer but just a (maybe valuable) tip:

would this be a better solution instead of the script ?



cp -an source_file target_file


The -a option preserves all attributes including timestamps.

The -n option prevents overwriting an existing file


[#28810] Thursday, January 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
finatch

Total Points: 49
Total Questions: 106
Total Answers: 94

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;