How to take care the parameter to a command?

September 26, 2011 at 12:59:01
Specs: N/A
Hello all !

I need some advices or examples on how to implement a Unix command that has a parameter.
For example, ls -l or grep -l
I was thinking execvp( ls, ls -l \n ).. looks kinda crazy
Just a side note, my parse.c file is set to look for " - " and then identify it as a parameter then save it in my struct.


Thanks in advance!


See More: How to take care the parameter to a command?

Report •

#1
September 26, 2011 at 13:25:50
I am assuming you are talking about calling a unix command from a "C" program? One way is to use interprocess communication and opening a pipe using the fopen command. This link discusses it:

http://www.computing.net/answers/pr...


Report •

#2
September 26, 2011 at 15:22:51
Thank you for the reply!
However, I think my problem is more on about the parameters of execvp()
I have already read the man page, but I am still confused. :(
Could you please explain to me what exactly are the 2 parameters that I should/can pass?

Thanks much.


Report •

#3
September 26, 2011 at 20:57:29
Using the exec functions isn't really my area of expertise, but it looks like you have to NULL terminate the arguments. Maybe this link will help you out:

http://cboard.cprogramming.com/c-pr...


Report •

Related Solutions

#4
September 27, 2011 at 00:39:43
execpv takes two parameters. The first is a null-terminated string containing the filename. The second is a pointer to an array of null-terminated strings containg the program's arguments. The first of these will be the program name.

This link gives an example of using it to execute the ls command.


Report •

#5
September 28, 2011 at 15:08:52
Mr. Nails,

Thank you very much for the reply & referral :D
Have a nice day~


Report •

#6
September 28, 2011 at 15:19:49
Hello Mr. ijack,

Thank you for pointing me to a useful example :)
However, I am not quite sure how to obtain the path?
Also, in this line of code
char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL\};
what is "/u/userid/dirname" suppose to be?

Thanks in advance!


Report •

#7
September 28, 2011 at 23:29:44
The path will just be where the executable is located - normally /bin, /usr/bin, or /usr/local/bin.

/u/userid/dirname in this example is just the name of the directory that you want listed; substitute whatever is appropriate in your case.


Report •

Ask Question