if (pid == 0) {
char * para[] = {"find", "./", "*.c", NULL};
execv("/usr/bin/find", para);
printf("execu failed: %s\n", strerror(errno)); //ls -l -R
exit(-1);
}
当我尝试使用 execv()
中的查找时,我收到消息 ./: ‘*.c’: No such file or directory
。当我在没有 *.c
参数的情况下运行 execv(find...)
时,我得到了编译我的程序的文件夹的内容,包括一些 .c文件 ./test.c, ./rock_paper_scissors.c, ./main.c
等等。当我直接在终端中运行命令 find ./*.c
时,我得到了预期的结果,即该目录中的所有 .c 文件。
是否有一些我没有看到的不同方式要求我通过 exec 使用查找?
最佳答案
改变
char * para[] = {"find", "./", "*.c", NULL};
成为
char * para[] = {"find", "./", "-name", "*.c", NULL};
来自 man find
:
-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so
-name a/b
will never match anything (you probably need to use -path instead).
关于c - 使用 execv 查找异常结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824872/