Why does this script work in the current directory but fail when placed in the path?

Posted by kiloseven on Stack Overflow See other posts from Stack Overflow or by kiloseven
Published on 2013-10-22T23:27:16Z Indexed on 2013/10/23 3:54 UTC
Read the original article Hit count: 229

Filed under:

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 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*

My question is, when I move this script into the path (verified by echo $PATH) why does it then fail?

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

© Stack Overflow or respective owner

Related posts about bash