[comp.unix.wizards] using #! and a different shell

kjh@visual1.jhuapl.edu (Kenneth J. Heeres) (02/22/91)

If I am writing my own shell command processor and I use the
  #! pathname 
capability to implicitly invoke it, what happens to the arguments that
were passed to the script???

ken
-- 
==========================================================================
Kenneth J. Heeres                             The Johns Hopkins University
INTERNET: kjh@visual1.jhuapl.edu              Applied Physics Laboratory
                                              Johns Hopkins Road
                                              Laurel, MD 20723-6099

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (02/22/91)

In article <1991Feb21.172233.8160@aplcen.apl.jhu.edu> kjh@visual1.jhuapl.edu (Kenneth J. Heeres) writes:
> If I am writing my own shell command processor and I use the
>   #! pathname 
> capability to implicitly invoke it, what happens to the arguments that
> were passed to the script???

1. Your subject line is nearly useless. ``Using #!'' is too general, and
``a different shell'' is irrelevant. Much better would be

  Subject: With #!pathname, what happens to the args passed to the script?

Always put as much information as possible into the subject line.

2. You didn't say what system you're using. For some questions it won't
matter---#!, for example, is a BSD feature, and your question has the
same answer under any BSD system. Also, anyone on the Internet can use
the domain name server or telnet to find out your host type. But your
next question may be Ultrix-specific, the name server is unreliable
enough with its current load, and a lot of USENET readers aren't on the
Internet.

Always give at least a minimal description of your environment.

3. Your article is in the wrong group. Do you think that you have enough
experience to judge whether a question will catch the interest of any
wizards? Probably not. Have you seen the group ``comp.unix.questions''?
Why don't you use it if you want to ask a question about UNIX?

Always try comp.unix.questions first unless you know your article is
more appropriate in another group.

4. There is almost certainly someone at Johns Hopkins who can answer
your question. It is easier to mail a question to root or your favorite
sysadmin than to post to the net. Always try local resources before
bothering thousands of people around the world.

5. You can probably answer the question by experimentation. Presumably
you have some program that depends on the behavior you asked about, so
why don't you test it for yourself?

6. The answer appears in the execve() man page on every BSD system I've
seen. Read the fucking manual.

7. The arguments are placed on the command line after the #!.

---Dan

jik@athena.mit.edu (Jonathan I. Kamens) (02/22/91)

% cat echo-args.c
#include <stdio.h>

main(int argc, char *argv[])
{
     if (! argc) {
          printf("There are no arguments.\n");
     }
     else {
          printf("The arguments are:");
          while (argc--) {
               printf(" %s", *argv++);
          }
          printf("\n");
     }
     exit(0);
}
% cat use-echo-args
#!/tmp/echo-args

exit 0
% ./use-echo-args foo bar baz
The arguments are: echo-args ./use-echo-args foo bar baz

This is on BSD 4.3; I doubt it's significantly different on other platforms
that allow #!.  I guess this is why "#!/bin/awk -f" works.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

grr@cbmvax.commodore.com (George Robbins) (02/22/91)

In article <1991Feb21.172233.8160@aplcen.apl.jhu.edu> kjh@visual1.jhuapl.edu (Kenneth J. Heeres) writes:
> 
> If I am writing my own shell command processor and I use the
>   #! pathname 
> capability to implicitly invoke it, what happens to the arguments that
> were passed to the script???

The outline is described in the man entry for execve().

The actual behavior of the Berkeley exec code sets up the
arguments as below and leaves stdin assigned to the script
at the line folling the #! stuff.

first arg:	interpreter specified after #!
next arg:	all arguments specified after above,
		*not* parsed for delimiters,
		skipped if no arguments
next arg:	command from command line
next arg:	first argument from command line, if any
next arg:	and so on...

The non-parsed arguments from the #! line can be
unfortunate since some programs you might wish to
use as "interpreters" prefer their arguments parsed.

ie "#! awk -f" does what's expected, "#! awk -F: -f" doesn't.

In non-hacked AT&T flavors of Unix, the setup is handled
by the shell, but the results should be equivalent.
-- 
George Robbins - now working for,     uucp:   {uunet|pyramid|rutgers}!cbmvax!grr
but no way officially representing:   domain: grr@cbmvax.commodore.com
Commodore, Engineering Department     phone:  215-431-9349 (only by moonlite)

guy@auspex.auspex.com (Guy Harris) (02/24/91)

>In non-hacked AT&T flavors of Unix, the setup is handled
>by the shell,

In non-hacked AT&T flavors of UNIX that don't have "Release 4" in their
name, "#!" isn't handled by the kernel nor by the shell.  In non-hacked
AT&T flavors of UNIX that *do* have "Release 4" in their name, "#!" is
handled by the kernel, in the same fashion that it's handled by BSD and
by many (probably most, if not all) other systems that have picked up "#!"
from BSD.