Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!apple!oliveb!mipos3!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.unix.questions Subject: Re: Forcing /bin/sh in a script under V/386 3.2 Korn shell Message-ID: <4692@omepd.UUCP> Date: 24 Jul 89 23:34:15 GMT References: <2318@wyse.wyse.com> <799@jonlab.UUCP> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 45 In-reply-to: jon@jonlab.UUCP (Jon H. LaBadie) In article <799@jonlab.UUCP>, jon@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@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/