This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Shebang


Totally off topic, but...

At Mon, 8 Mar 2004 20:50:13 +0000 (UTC), prj@po.cwru.edu wrote:
> > I suggest you write a wrapper program like this (only in C - you can't
> > use a shell script as an #! interpreter):
> 
> But you can do:
> #!/usr/bin/env shell-script
> ... input interpreted by shell-script here ...

AFAIK not portably you can't:

217 [ldt-sj3-010] tmp % cat > shcat
#!/bin/sh
while read x; do
        echo $x;
done
218 [ldt-sj3-010] tmp % chmod +x !$
chmod +x shcat
219 [ldt-sj3-010] tmp % shcat
foo
foo
220 [ldt-sj3-010] tmp % cat > doshcat
#!/usr/bin/env shcat
foo
bar
221 [ldt-sj3-010] tmp % chmod +x doshcat 
222 [ldt-sj3-010] tmp % ./doshcat 
223 [ldt-sj3-010] tmp % ./doshcat
blah                                    <- entered on stdin
blah
224 [ldt-sj3-010] tmp % 

(that's on a linux system.)

To quote the solaris manual page for execve:

     An interpreter file begins with a line of the form

          #! pathname [arg]


     where pathname is the path of the interpreter, and arg is an
     optional argument. When an interpreter file is executed, the
     system  invokes  the  specified  interpreter.  The  pathname
     specified  in  the interpreter file is passed as arg0 to the
     interpreter. If arg was specified in the  interpreter  file,
     it is passed as arg1 to the interpreter. The remaining argu-
     ments to the interpreter are arg0 through argn of the origi-
     nally  exec'd  file.  The interpreter named by pathname must
     not be an interpreter file.

note that last sentence.

I forget the exact rules of this (it's been, oh, 8 years since i last
touched the script exec code I wrote for NetBSD), but in a nutshell,
"no."  8-)


chris


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]