[comp.unix] Shell scripts starting with "#"

df@sei.cmu.edu (Dan Farmer) (07/14/90)

[Moderators Note:-
To save a deluge of mail and postings, the answer is to
put a ':' by itself on line one of the script.  For example;
:
# This is the second line of a bourne-shell script.
This may not work on absolutely *every* variant, but it should cover most.
						- Der]

  Does System V (or some versions of Sys V, or any version of the Un*x,
for that matter) execute a shell script under csh if it begins with a
"#" (for instance if you explicitly say "#!/bin/sh")?  I've been told
this, but can't confirm it....
  If this is true, what is the best portable way to always have a script
execute under /bin/sh?  (don't say "/bin/sh script"....)

  thanks,

  - dan
    df@sei.cmu.edu

pd@x.co.uk (Paul Davey) (10/10/90)

   [Moderators Note:-
   To save a deluge of mail and postings, the answer is to
   put a ':' by itself on line one of the script.  For example;
   :
   # This is the second line of a bourne-shell script.
   This may not work on absolutely *every* variant, but it should cover most.
						   - Der]

     Does System V (or some versions of Sys V, or any version of the Un*x,
   for that matter) execute a shell script under csh if it begins with a
   "#" (for instance if you explicitly say "#!/bin/sh")?  I've been told
   this, but can't confirm it....
     If this is true, what is the best portable way to always have a script
   execute under /bin/sh?  (don't say "/bin/sh script"....)


Some C-shells that don't recognise this convention will run
bourne-shell scripts correctly if you do the following.
	set shell = /bin/sh 
NOTE lower case and NOT setenv SHELL /bin/sh

Machines that don't recognise #! seem to be fairly rare now though.


--
 Regards,			 pd@x.co.uk          IXI Limited
	Paul Davey		 pd@ixi.uucp         62-74 Burleigh St.
				 ...!uunet!ixi!pd    Cambridge  U.K.
 "These are interesting times"   +44 223 462 131     CB1  1OJ      

scott@grlab.UUCP (Scott Blachowicz) (10/17/90)

>    [Moderators Note:-
>    To save a deluge of mail and postings, the answer is to
>    put a ':' by itself on line one of the script.  For example;
>    :
>    # This is the second line of a bourne-shell script.
>    This may not work on absolutely *every* variant, but it should cover most.
> 						   - Der]
But the ":" doesn't work on systems that support "#!". My latest
solution (having recently been forced to work with a "straight"...read
archaic/restrictive... System V system) looks like this:
   #!/bin/sh
   : || exec /bin/sh $0 $argv:q

Regarding Re: Shell scripts starting with "#"; pd@x.co.uk (Paul Davey) writes:
> ...
> Machines that don't recognise #! seem to be fairly rare now though.

Don't know about that...doesn't SCO have a few customers?
--
Scott Blachowicz                E-mail:  scott%grlab.UUCP@hpubvwa.HP.COM
USPS:  Graphicus                 ..or..  ...!hpubvwa!grlab!scott
       150 Lake Str S, #206     VoicePh: 206/828-4691
       Kirkland, WA USA 98033   FAX:     206/828-4236

tag@mtunf.ATT.COM (Tom Gillespie) (10/18/90)

In AT&T System V Release 3 for 3B2, neither

#!/bin/sh

nor

: use /bin/sh

will force Bourne shell execution; Korn shell Version 11/16/88 simply ignores
either one. (csh does not exist for 3B2 as far as I know).

For 6386, which does have csh,

: use /bin/sh

works reliably; again, Korn shell (same version) ignores it.

My favorite workaround:

(alias) >/dev/null 2>&1 && exec /bin/sh $0 $@

since 'alias' is a built-in command in ksh.  This is the test that Larry Wall's
metaconfig builds in to the Configure script to warn ksh users that Bourne
shell should probably be used instead.


Tom Gillespie
tag@mtunf.att.com
(201) 576-2338

peter@ficc.ferranti.com (Peter da Silva) (10/21/90)

So if you do:

	#!/bin/sh
	: || exec /bin/sh $0 $argv:q
	(alias) >/dev/null 2>&1 && exec /bin/sh $0 "$@"

It'll work for the bourne shell, the korn shell, and the c shell, whether
or not you have #! working. Geeze.
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com

gwc@root.co.uk (Geoff Clare) (10/24/90)

In <2227@megadon.UUCP> peter@ficc.ferranti.com (Peter da Silva) writes:
>So if you do:
>	#!/bin/sh
>	: || exec /bin/sh $0 $argv:q
>	(alias) >/dev/null 2>&1 && exec /bin/sh $0 "$@"
>
>It'll work for the bourne shell, the korn shell, and the c shell, whether
>or not you have #! working. Geeze.

But only if your script treats a single null argument the same as no
arguments, and doesn't have any special characters in it's name.
If you want to be that little bit more robust, replace Peter's

	$0 "$@"
with
	"$0" ${1+"$@"}

It's probably also worth pointing out that mechanisms of this type
rely on the process which executes the script being well behaved.
If executed from a program which does an

	execl("/somewhere/your_script", "ha ha, gotcha!", (char *)0);

or from a shell whose local search path is different to the value of
PATH it exports, then all bets are off.
-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, Hayne Street, London EC1A 9HH, England.   Tel: +44-71-315-6600