[comp.unix.questions] Recovery scripts: use $@

avr@hou2d.UUCP (06/24/87)

In article <1500@ho95e.ATT.COM>, wcs@ho95e.ATT.COM (Bill.Stewart) writes:
> I generally write all my scripts in Bourne Shell, because I know they'll work
> (relatively) portably.  When it's important to use ksh, I use the following:
> 	if [ "$RANDOM" = "$RANDOM" ] 
> 	then ## recovery script to feed itself to ksh
> 		ksh -c "$0 $*"
> 		exit "$?"
> 	fi
> 
> The simpler approach of checking $SHELL doesn't work; SHELL is exported,
> and some programs will drop into /bin/sh by default.  ksh updates $RANDOM each
> time it uses the value, so "$RANDOM" = "$RANDOM" will never be true in ksh,
> and will (presumably) always be true in other shells.
> 
> Writing a good recovery script is hard; if the arguments to the program
> contain white space or metacharacters, the $* will trash them.

Use $@ instead of $*.
			Adam Reed (hou2d!avr)

simon@its63b.ed.ac.uk (Simon Brown) (06/27/87)

In article <1444@hou2d.UUCP> avr@hou2d.UUCP (Adam V. Reed) writes:
>In article <1500@ho95e.ATT.COM>, wcs@ho95e.ATT.COM (Bill.Stewart) writes:
>> I generally write all my scripts in Bourne Shell, because I know they'll work
>> (relatively) portably.  When it's important to use ksh, I use the following:
>> 	if [ "$RANDOM" = "$RANDOM" ] 
>> 	then ## recovery script to feed itself to ksh
>> 		ksh -c "$0 $*"
>> 		exit "$?"
>> 	fi
>> 
>> Writing a good recovery script is hard; if the arguments to the program
>> contain white space or metacharacters, the $* will trash them.
>
>Use $@ instead of $*.
>			Adam Reed (hou2d!avr)

This will not work for a "sh -c command" type thing - the ``command'' must
be a single word, and "$@" splits up into lots of separate words - so, better
would be to use
	ksh "$0" "$@"
instead.

In fact, Use ${1+"$@"} instead of "$@", because the latter gets it wrong if
there are no positional parameters set (it subsitutes the empty string instead
of nothing at all). I believe ksh has this ``fixed''.

	%{
	    Simon!
	%}


-- 
----------------------------------
| Simon Brown                    | UUCP:  seismo!mcvax!ukc!its63b!simon
| Department of Computer Science | JANET: simon@uk.ac.ed.its63b
| University of Edinburgh,       | ARPA:  simon%its63b.ed.ac.uk@cs.ucl.ac.uk
| Scotland, UK.                  |
----------------------------------     "Life's like that, you know"

avr@hou2d.UUCP (Adam V. Reed) (07/05/87)

In article <506@its63b.ed.ac.uk>, simon@its63b.ed.ac.uk (Simon Brown) writes:
> In article <1444@hou2d.UUCP> avr@hou2d.UUCP (Adam V. Reed) writes:
> >In article <1500@ho95e.ATT.COM>, wcs@ho95e.ATT.COM (Bill.Stewart) writes:
> >> I generally write all my scripts in Bourne Shell, because I know they'll work
> >> (relatively) portably.  When it's important to use ksh, I use the following:
> >> 	if [ "$RANDOM" = "$RANDOM" ] 
> >> 		ksh -c "$0 $*"
> >> 		exit "$?"
> >> 	fi
> >> Writing a good recovery script is hard; if the arguments to the program
> >> contain white space or metacharacters, the $* will trash them.
> >Use $@ instead of $*.
> This will not work for a "sh -c command" type thing - the ``command'' must
> be a single word, and "$@" splits up into lots of separate words - so, better
> would be to use
> 	ksh "$0" "$@"

If $0 is not in the current directory, you will get "file not found".
The correct command to put in recovery scripts using $@ is
	ksh `ksh -c "whence $0"` $@
This might not work for you, because in some early versions of ksh
"whence" and/or $@ did not always work correctly. I understand that
Dave Korn has already fixed this.
					Adam Reed (hou2d!avr)