我的execvp
未运行ls -l *.c
命令。我尝试使用两种方法:
\bin\ls
中的一个。#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
char *cmdargs[] = { "ls", "-l", "*.c", NULL };
pid_t pid;
pid = fork();
if (pid == 0)
execvp("\bin\ls", cmdargs);
else
{
wait(NULL);
printf("Child terminates\n");
}
return 0;
}
输出:
ls: *.c: No such file or directory
Child terminates
cmdargs[0]
而不是文件路径。#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
char *cmdargs[] = { "ls", "-l", "*.c", NULL };
pid_t pid;
pid = fork();
if (pid == 0)
execvp(cmdargs[0], cmdargs);
else
{
wait(NULL);
printf("Child terminates\n");
}
return 0;
}
输出:
ls: *.c: No such file or directory
Child terminates
[当我只运行命令ls -l *.c
时,它的确显示了所有以.c
结尾的文件。 Execvp没有显示文件。有一个与此相关的问题,但这并没有帮助我。
星号模式*
由外壳执行,但由ls
执行。
例如,您可以使用exec
。