1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
K7AAY
  • 48
  • 16
  • How did you execute it? Also, did you mark it as executable? – sidyll Oct 22 '13 at 23:31
  • 1
    Well, in /user/bin it is in the path so I just entered safe.sh filename And, yes, I did chmod +x safe.sh to mark it as executable. – K7AAY Oct 22 '13 at 23:33

2 Answers2

3

The . command is not normally used to run standalone scripts, and that seems to be what is confusing you. . is more typically used interactively to add new bindings to your environment (e.g. defining shell functions). It is also used to similar effect within scripts (e.g. to load a script "library").

Once you mark the script executable (per the comments on your question), you should be able to run it equally well from the current directory (e.g. ./safe.sh filename) or from wherever it is in the path (e.g. safe.sh filename).

You may want to remove .sh from the name, to fit with the usual conventions of command names.

BTW: I note that you mistakenly capitalize If in the script.

danfuzz
  • 4,253
  • 24
  • 34
  • 1
    My point is: It is a bad idea to ever try running a command script using `.`, since the semantics are very different. – danfuzz Oct 22 '13 at 23:54
  • 1
    Thank you for noting the typo; using a virtual KVM-over-IP rig so I have to shift mental gears between Linux and The Unspeakable OS. I'd caught that on the Linux system when I was pasting that in, but to reach the web, I must use The Unspeakable. – K7AAY Oct 22 '13 at 23:54
  • 1
    Does it work without `.` from the current directory? – danfuzz Oct 22 '13 at 23:57
  • That's normal, assuming the current directory isn't in your path. (There's a big difference between `. foo` and `./foo`.) – danfuzz Oct 23 '13 at 00:03
  • Another weird thing: The dash in the script you pasted is an "en dash" and not a regular hyphen. – danfuzz Oct 23 '13 at 00:08
  • Having fixed that issue, near as I can tell the script works for me. I tried on both OS X and Linux. – danfuzz Oct 23 '13 at 00:13
2

The error bad interpreter: Text file busy occurs if the script is open for write (see this SE question and this SF question). Make sure you don't have it open (e.g. in a editor) when attempting to run it.

Community
  • 1
  • 1
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151