Execvp With Quotation Marks
Hello,
I am trying to run a command using execvp for example: execvp ("grep", arg_list); but if one of the arguments is with the Quotation marks - for example grep "int main" test.c, or even grep "main" test.c it doesnt run the command. if i'm doing it through unix shell it works great. any ideas? |
Code:
#include <stdio.h> Why use quotation marks? Each argument that you pass in the arg_list is treated as a single argument. If you try to combine -n and -H in one argument, grep will object. |
Quote:
i need to find the reason for not running with quotation marks. |
OK, I don't seem to understand the problem. I replaced the int main by a \" and it finds it perfectly (3 lines returned).
|
|
I'm lost. What do you mean by real execvp?
The above code was written using the man page that you gave. Is it possible to post part of your code? |
This is a small Unix Shell
i can't seem to run the execvp() with the command - grep "main" test.c compile the shell, and run the command: grep text textfile and then : grep "text" textfile it can't run with the quotation marks. i don't know why... Code:
#include <stdio.h> |
I think that your problem is in the way you use strtok.
To parse a line, you have to call strtok more than once; the first time with read_line as argument and consecutive calls with NULL instead of read_line. The code below demonstrates how to use it. Code:
int main() please note that the use of strtok is not advised unless you know what you're doing; the man-page states: never use these functions |
Add this to your code, and try to run this:
1. grep text text_file - working 2. grep "text" text_file - Not working Code:
|
OK, finally see what you were trying to tell me.
When you add the quotes, it only finds the word if it's surrounded by quotes. And that's slightly different from the grep behaviour on the commandline. On the command line, you use the quotes to create one argument from words separated by spaces. So those are not literal quotes. The shell will read this and create one argument from it which will be passed to grep as you do in the code. But the shell will strip off the quotes as it does not need it (it's just something for the shell). In your program example, you literally look for a string including the quotes as you pass the quotes in the argument. On the commandline this would be grep \"text\" text_file. Hope this clears it. I think you have to do a bit more pre-processing before passing the string to execute. |
Thanks for your answer! it help a lot!
|
All times are GMT -5. The time now is 01:41 AM. |