ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
When you save this as 'execvp.c', compile it and run it, it finds int main. It also gives the filenames and line numbers.
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.
Last edited by Wim Sturkenboom; 11-21-2005 at 10:12 AM.
When you save this as 'execvp.c', compile it and run it, it finds int main. It also gives the filenames and line numbers.
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.
I know all that but i need to find a way to use quotation marks through execvp.
i need to find the reason for not running with quotation marks.
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>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
void execute(char **argv,int background_flag)
{
pid_t pid;
int status;
int j=0;
int exe_process;
if ((pid = fork()) < 0) /* fork a child process */
{
printf("ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) /* for the child process: */
{
printf("\033[42m\033[1m[%d]\033[m\033[m\n",getpid());
if (execvp(*argv, argv) < 0) /* execute the command */
{
printf("ERROR: exec failed\n");
exit(1);
}
}
else /* for the parent: */
{
if (background_flag==0)
{
while (wait(&status) != pid);
if (WIFEXITED (status))//WIFEXITED
{
printf("\033[45m\033[1m[%d]: Done\033[m\033[m\n",pid);
}
else
{
printf ("the child process exited abnormally from signal %d\n",WTERMSIG(status ) );
}
}
}
}
int main(void)
{
char read_line[1024];
char* arg_list[10];
int i,background_flag,status;
pid_t pid;
while (1)
{
i=0;
background_flag=0;
// printf("\033[44m\033[1m%s >\033[m\033[m",getenv("PWD"));
printf("\033[44m\033[1mUser_Shell:\033[m\033[m");
gets(read_line);
//clean zombie proccess
if ((pid=waitpid(-1,&status,WNOHANG))!=-1)
printf("\033[45m\033[1m[%d]: Done\033[m\033[m\n",pid);
if (read_line[0]!=NULL)
{
arg_list[i] = strtok(read_line," ");
while (arg_list[i]!= NULL)
{
if (strcmp(arg_list[i],"&")) i++;
else background_flag=1;
arg_list[i]=strtok(NULL," """);
}
if (strcmp(arg_list[0], "exit") == 0)
exit(0);
execute(arg_list,background_flag);
}
}
return 0;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.