Forcing /bin/sh in a script under V/386 3.2 Korn shell

Randal Schwartz merlyn at iwarp.intel.com
Tue Jul 25 09:34:15 AEST 1989


In article <799 at jonlab.UUCP>, jon at jonlab (Jon H. LaBadie) writes:
[lotsa stuff deleted]
| Combining these comments and using the conditional operator rather than
| an if statement; plus quoting $0 on general principles, we have:
| 
|   [ "${RANDOM}" = "${RANDOM}" ] || exec /bin/sh -c "${0}" ${@:+"${@}"}
| 
|   # what you want sh to do
| 
| Now a question for Bob.  Can you let me know the need for the
| 
| 	`whence $0`
| 
| construction in your solution.  On inspection, it seems that $0 would
| be sufficient as the script was invoked that way to begin with.  But
| I am certain I am overlooking some subtle situation in which that
| is not the case.

I can think of a subtle situation: the $ENV file of ksh sets a
different searchpath, or maybe even an alias, that would not be picked
up by the corresponding /bin/sh started later.

But also, you DON'T want the -c on there.  The command line would then
consist of just the command name, not the arguments.  (The shell
tosses arguments on a -c command line...)

Thus, a more secure initial line would be something like:

[ "X$RANDOM" = "X$RANDOM" ] || exec /bin/sh "`whence \"$0\"`" ${@+"$@"}

Note the "X"'s on the test... if $RANDOM is not set, the previous
example could raise havoc in the test.  I also don't believe the ":"
is required in the ${@..} construct, since the colon merely selects
between testing for $@ set, and $@ "set and non null", and the bug
comes about when $@ is *not* set (such as when there are no
arguments).

My solution also allows for a command name with whitespace in it :-)

Just another shell hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel, Hillsboro, Oregon, USA                           |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.unix.questions mailing list