From owner-freebsd-questions Sat Feb 8 13: 0:48 2003 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D4EA737B401 for ; Sat, 8 Feb 2003 13:00:42 -0800 (PST) Received: from mta6.snfc21.pbi.net (mta6.snfc21.pbi.net [206.13.28.240]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7B2C243F3F for ; Sat, 8 Feb 2003 13:00:42 -0800 (PST) (envelope-from mbsd@pacbell.net) Received: from atlas ([64.166.22.106]) by mta6.snfc21.pbi.net (iPlanet Messaging Server 5.1 HotFix 1.6 (built Oct 18 2002)) with ESMTP id <0HA000KHWD15P2@mta6.snfc21.pbi.net> for questions@freebsd.org; Sat, 08 Feb 2003 13:00:42 -0800 (PST) Date: Sat, 08 Feb 2003 13:00:41 -0800 (PST) From: =?ISO-8859-1?Q?Mikko_Ty=F6l=E4j=E4rvi?= Subject: Re: #!/bin/sh & execve In-reply-to: <200302081544.h18FiPTu024398@en26.groggy.anc.acsalaska.net> X-X-Sender: mikko@atlas.home To: abc@anchorageinternet.org Cc: freebsd-questions Message-id: <20030208125353.V70415-100000@atlas.home> MIME-version: 1.0 Content-type: TEXT/PLAIN; charset=US-ASCII Content-transfer-encoding: 7BIT Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sat, 8 Feb 2003 abc@anchorageinternet.org wrote: > say i have 2 scripts, scriptA and scriptB. > > scriptA > ------- > #!/bin/sh ./scriptB 1 2 3 > > scriptB > ------- > #!/bin/sh > > echo 0:$0 > echo 1:$1 > echo 2:$2 > echo 3:$3 > > -- > > $ ./scriptA > > $0:./scriptB > $1:1 > $2:2 > $3:3 > > -- > > according to execve(2), only a single [arg] should be recognized: > > #! interpreter [arg] > > When an interpreter file is execve'd, the system actually execve's the > specified interpreter. If the optional arg is specified, it becomes the > first argument to the interpreter, and the name of the originally > execve'd file becomes the second argument; otherwise, the name of the > originally execve'd file becomes the first argument. The original argu- > ments are shifted over to become the subsequent arguments. The zeroth > argument is set to the specified interpreter. > > so the argv[] array in execve() should be loaded as: > > argv[0]=sh, argv[1]=scriptB, argv[2]=scriptA, and > argv[3...]=command line args passed to scriptA. > > i read many many execve() man pages, and it seems like this > is the way things should be. but in practice, it appears on > many unix's, argv[] gets loaded additionally with any options > given to a script (which is given as the "[arg]" to the interpreter) > on the 1st line of a script. > > can anyone tell me if this is "proper", and why or why not? > there doesn't seem to be consistency across unix's. > some ignore, or give an error if more than one > "[arg]" exists on the 1st line of a script. The only thing I can find in IEEE Std 1003.1-2001 (aka SUSv3) is "If the first line of a file of shell commands starts with the characters "#!", the results are unspecified." which would indicate that there is no "proper" way of doing this. You may also want to have a look at bin/16393; at the bottom is a list of how some unices handle the situation. Your best bet at trying to be portable is to use at most one argument, no whitespace and no "#". The PR: $.02, /Mikko To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message