Clunch protection

Chuck Privitera crp at ccivax.UUCP
Fri Oct 19 23:50:12 AEST 1984


> brl-tgr!gwyn:
> 
> > #!/bin/sh
> > on line 1 will let the kernel exec the script using the correct shell
> > no matter what shell the user is running at the time.
> 
> 2.  The "#!" construct is not recognized on all UNIX systems.  Some may respond
>     that anyone who'd continue running a system that fails to recognize "#!"
>     "ougtht to" upgrade.  Alas, in the real world, there are legitimate
>     economic, political, and legal reasons for staying with older versions of
>     UNIX; those using older versions are ill-served by folks who would lecture
>     them to upgrade rather than try to keep their needs in mind.
> 

We just ran into this on our Power5's. The kernel on the Power5
doesn't understand "#!your_favorite_interpreter", and we had some
scripts that took advantage of this with #!/bin/sh.  They no longer
worked. Trivial changes to texec() in sh.exec.c of the csh solved
the problem. Similar changes could be applied to function execs()
in service.c for the Bourne shell (as is done on the VAX save that
it should read a whole line and look for #!whatever instead of
assuming '#' means use csh.)

> 3.  Even if the "#!/bin/sh" line is present at the start of a shell script,
>     "/bin/sh" will NOT be used to interpret the script if the script is
>     executed using the command
> 	csh script
>     (This is, at any rate, true on 4.1bsd and 4.2bsd).  "Clunch protection"
>     lines are designed to guard against this possibility.  Some may respond
>     that anyone who'd use the above command is a clunch; a moment's
>     reflection. . .
> --

The C-shell (and Bourne shell) on BSD systems have a primitive way
of deciding which shell to execute if the kernel doesn't get it right.
Both shells check the error returned from a failed exec and if it
is ENOEXEC (exec format error) they check the first byte of the file.
If it is a '#', BOTH shells assume that it must be a C-shell script.
Not too cool since '#' is the Bourne shell's comment character too. 
Granted you could use ':' for comments, but then you are subject
to some of the Bourne shell's features you may not intend to evoke.
(e.g. Quoting across lines) This is one reason I personally prefer
to use '#' for comments instead of ':'. Try putting this in a file
and running it:

	:
	: This is Chuck's test sh script
	:
	echo test

No output will result, because the shell is looking for a closing
"'". Whereas the following will output "test" as was intended:

	: Start with one of these to force using the Bourne shell.
	# This is Chuck's second test sh script
	#
	echo test



More information about the Comp.unix mailing list