[comp.lang.c] #! command arguments

david@wubios.wustl.edu (David J. Camp) (09/30/89)

I am thinking about implementing a script that can be invoked via the #!
syntax on the first line of a script.  I want it to parse any arguments
on that same line, if present, in addition to the arguments on the
invoking command line.  I have been experimenting with a simple C
program that prints out its arguments.  It seems that if no arguments
are present, the argv[0] is the shell, and argv[1] is the script.  If
arguments are present, then argv[0] is still the shell, but argv[1] is
the entire argument list and argv[2] is the script.

What I want to know is, what is the easiest way to determine if
arguments are present.  Since other arguments may follow from the
original command line, the argc variable is not sufficient information.
Since I do not know in advance what the name of the script is, I cannot
compare it against a list of known scripts.  Assuming free-form
arguments, some of which may be filenames, I cannot simply look for a
'-' character, although that may be what I require if a better
alternative is not presented.

Any help will be appreciated, and please reply directly to me.  Thank
you,  -David-


#include <stdio.h>
#include <string.h>

void main (argc, argv)
int argc;
char * argv [];

{
int arg;

for (arg = 0; arg < argc; arg++)
    {
    fprintf (stderr, "argv [%d] = %s\n", arg, argv [arg]);
    }
}
-- 
Bitnet:   david@wubios.wustl                ^      Mr. David J. Camp
Internet: david%wubios@wucs1.wustl.edu    < * >    Box 8067, Biostatistics
uucp:     uunet!wucs1!wubios!david          v      660 South Euclid
Washington University (314) 36-23635               Saint Louis, MO 63110