【发布时间】:2020-07-15 10:51:17
【问题描述】:
我的execvp 没有运行ls -l *.c 命令。我尝试使用两种方法:
- 我的 ls 所在的文件路径位于
\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 没有向我显示文件。有一个与此相关的问题,但这对我没有帮助。
【问题讨论】:
标签: c unix system system-calls systems-programming