execv
takes the command as the first parameter so you want "usr/bin/grep"
not argv[0]
which is the program you are running. Also remove the first parameter from argv
.
I would also check that grep
is in /usr/bin
it's often in /bin
.
質問
argv = {'/usr/bin/grep', '/usr/bin/grep', '-ri', 'test', '.', 0}
pid_t pid = fork();
if (pid == 0) {
execv(argv[0], argv);
exit(0);
}
else {
wait(NULL);
}
This is what I have for my code. I am simply trying to get the grep command to run. The problem I run into is that it thinks the second /usr/bin/grep is the search pattern.
I have tried to change the argv array to only contain one /usr/bin/grep, but that errors out and does not run the grep. Any help?
解決
execv
takes the command as the first parameter so you want "usr/bin/grep"
not argv[0]
which is the program you are running. Also remove the first parameter from argv
.
I would also check that grep
is in /usr/bin
it's often in /bin
.