You are not logged in.
This is an extremely simple question. Please forgive me for being a noob. I am writing an application in C (with GCC) called GAPE and need to be able to pass it parameters through a shell script (/usr/bin/gape) that can determine how it runs. To start with easy stuff, I want the end-user to be able to type "gape -V" to output the version of the program. How do I do that? I understand that I need to put the shell script in /usr/bin...where should I put the actual gcc-compiled gape application? Or do I even need a shell script? Can I just put the GAPE binary in /usr/bin and pass parameters to it directly? If so, how do I do that in C? Any help is greatly appreciated.
Last edited by tony5429 (2008-03-10 12:00:21)
Offline
include <stdio.h>
main(int argc, char *argv[])
{
int i;
for(i = 1; i < argc; i++) //argc = the number of arguments
{
printf("param nr %d: %s\n", i, argv[i]); // argv[i] contain the arguments, with 0 being the program name (anyone may correct me here)
}
}
[jaqob@p238 c++-egna]$ ./a.out 1 2 3
param nr 1: 1
param nr 2: 2
param nr 3: 3
[jaqob@p238 c++-egna]$ ./a.out -h -V -zxvf
param nr 1: -h
param nr 2: -V
param nr 3: -zxvf
A very simple program that prints the parameters
And yes, you can put the program in /usr/bin/ without a shellscript
Last edited by JaQoB (2008-03-05 18:09:34)
Offline
Thanks! I will give it a shot!
Offline
argv[0] isn't fixed to the program name, it could be any string that the caller pass as the first arg. The shell passes
the program name but u can call a program with a exec* system call and pass whatever u want to for the 0 argument
Are u listening?
Offline
argv[0] isn't fixed to the program name, it could be any string that the caller pass as the first arg. The shell passes
the program name but u can call a program with a exec* system call and pass whatever u want to for the 0 argument
Thanks for the correction
Offline
If you are using C, look at the getopt and getopt_long functions. They simplify the handling of options greatly.
Offline
Thanks for all the support! Now that I've done some work on the program, I am running into another problem. Here is my code...
long x;
x = (long)strchr("hello/bye", 47);
printf("x: %d\n", x);
47 is the ASCII code of '/' so I figure I should get the pointer, '5' for the character's location in the string but instead I seem to get 4196713 every time...
Any ideas?
Offline
strchr returns a char* to the match. Example usage:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
return 0;
}
Offline
Once again, thank you very much! It is exactly what I needed.
Offline