Passing Strings to a shell

WeeZer51402

Registered
This may be a somewhat trivial question and i havent researched it too much but is there a function that will pass a string to a shell, for instance say i had a stdio program and i decided i wanted to add the option of allowing the user to enter commands to run in the backround, some i know you can send commands to a shell with the system(); function, but how do you pass it a variable?
 
Originally posted by WeeZer51402
i know you can send commands to a shell with the system(); function, but how do you pass it a variable?
I don't use system(), but you can fork a process, with fork(), then in the child branch you execlp, execve, ..., a command. To build your command, you initialise a buffer and fill it with the parameters supplied by the user using strcpy.

Some examples are here:
http://micmacfr.homeunix.org/cocoa.shtml.en#finkessai
 
Err, if you're just wanting to pass it variables when it is first launched, then the commands are in the argv[] array. argv[0] will always be the command name, but anything else a user types after the command will be listed there. The commands will be separated by any spaces, so like if I entered someProg do something, the argv array would be: argv[0]="someProg", argv[1]="do", and argv[2]="something". Note that all the arguments are going to be strings, so if you need numbers, you'll have to convert the string to ints or floats or whatever you need.

If you need to get how many arguments were passed into the program, argc will let you know. It will always be at least one, since the command name is always considered one of the arguments passed to the program.

Just in case you're wondering where I'm getting argc and argv[] from, they're declared in the main() function: int main(int argc, const char *argv[])
 
Back
Top