Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-21-2005, 06:48 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Rep:
|
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?
|
|
|
|
11-21-2005, 10:10 AM
|
#2
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
Code:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char *cmd[]={"grep","-n", "-H", "int main","execvp.c",NULL};
if(execvp("grep",cmd)<0)
{
perror("execvp");
return -1;
}
return 0;
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.
|
|
|
|
11-21-2005, 12:28 PM
|
#3
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Original Poster
Rep:
|
Quote:
Originally posted by Wim Sturkenboom
Code:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char *cmd[]={"grep","-n", "-H", "int main","execvp.c",NULL};
if(execvp("grep",cmd)<0)
{
perror("execvp");
return -1;
}
return 0;
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.
|
|
|
|
11-21-2005, 02:15 PM
|
#4
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
OK, I don't seem to understand the problem. I replaced the int main by a \" and it finds it perfectly (3 lines returned).
|
|
|
|
11-21-2005, 03:05 PM
|
#5
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Original Poster
Rep:
|
your prog works just fine, try to run it with the real execvp() command...
execvp
|
|
|
|
11-22-2005, 01:36 AM
|
#6
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
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?
|
|
|
|
11-24-2005, 03:55 PM
|
#7
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Original Poster
Rep:
|
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;
}
Last edited by amitbern; 11-24-2005 at 03:56 PM.
|
|
|
|
11-25-2005, 02:19 AM
|
#8
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
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()
{
char read_line[100], char *arg_list[10];
int cnt=0;
strcpy(read_line,"hello, testing 1 2 3");
arg_list[cnt]=strtok(read_line," ");
while(arg_list[cnt]!=NULL)
{
printf("%s\n",arg_list[cnt]);
arg_list[++cnt]=strtok(NULL," ");
}
return 0;
}
// edit:
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
Last edited by Wim Sturkenboom; 11-25-2005 at 02:44 AM.
|
|
|
|
11-25-2005, 04:19 AM
|
#9
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Original Poster
Rep:
|
Add this to your code, and try to run this:
1. grep text text_file - working
2. grep "text" text_file - Not working
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char read_line[100];
char *arg_list[10];
int cnt=0;
gets(read_line);
arg_list[cnt]=strtok(read_line," ");
while(arg_list[cnt]!=NULL)
{
printf("%s\n",arg_list[cnt]);
arg_list[++cnt]=strtok(NULL," ");
}
execvp(arg_list[0], arg_list);
return 0;
}
Last edited by amitbern; 11-25-2005 at 04:22 AM.
|
|
|
1 members found this post helpful.
|
11-25-2005, 07:42 AM
|
#10
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
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.
|
|
|
|
11-25-2005, 10:28 AM
|
#11
|
|
LQ Newbie
Registered: Nov 2005
Posts: 12
Original Poster
Rep:
|
Thanks for your answer! it help a lot!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:25 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|