Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
what about #!/usr/bin/env sh –  user85164 Oct 23 '13 at 0:16

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer

"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.

share|improve this answer
    
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 '13 at 18:32
    
Glad it's working now, you may be right about why it wasn't showing up. –  andy Oct 23 '13 at 23:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.