0

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.

1
  • what about #!/usr/bin/env sh
    – user85164
    Oct 23, 2013 at 0:16

2 Answers 2

1

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

1

"Text file busy" implies that the script is being used elsewhere. Find out what is opening the file using:

lsof | grep safe.sh

Then stop that process/es as necessary.

2
  • Well, a day later, on retesting, it works AOK. Go figure. No changes, the lsof line from Andy shows nothing is typing up the file, so I figure something was typing up the file and time led it to be released by whatever process was holding it.
    – K7AAY
    Oct 23, 2013 at 18:32
  • Glad it's working now, you may be right about why it wasn't showing up.
    – andy
    Oct 23, 2013 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .