execvp命令未运行ls -l * .c

问题描述 投票:0回答:1

我的execvp未运行ls -l *.c命令。我尝试使用两种方法:

  1. 带有我的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
  1. 我使用的第二种方法是添加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
1个回答
0
投票

星号模式*由外壳执行,但由ls执行。

例如,您可以使用exec

© www.soinside.com 2019 - 2024. All rights reserved.