如何将命令行从shell脚本传递到C程序的命令行参数?

如何将命令行从shell脚本传递到C程序的命令行参数?,c,C,在C程序中,我很难将命令行从shell脚本传递到命令行参数。目前,我只能在程序中逐个传递命令行作为参数 int main(int argc, char ** argv) { int pipefd[2]; // Array for pipe pid_t processChild; // Id fork pipe(pipefd); // Demarrage du pipe avec l'array if (processChild = fork()==0){ close(1); //

在C程序中,我很难将命令行从shell脚本传递到命令行参数。目前,我只能在程序中逐个传递命令行作为参数

int main(int argc, char ** argv) {

int pipefd[2]; // Array for pipe

pid_t processChild; // Id fork

pipe(pipefd); // Demarrage du pipe avec l'array

if (processChild = fork()==0){
    close(1); // stdout close
    close(pipefd[0]); // close read
    dup2(pipefd[1],1); // Copy writting to write in pipe

    int d = execvp(argv[1], &argv[1]); // Exec command in param

    if (d == -1){ // Verif
        printf("Erreur \n");
        exit(0);
    }
}
else{
    char tmp[32]; //  Tampon for the pipe read
    int nbrBytes;
    int totBytes;
    close(pipefd[1]); // Closing writting

    system("echo --- RESULT ---"); // Show result
    while((nbrBytes = read(pipefd[0], tmp, sizeof(tmp))) > 0){ // Reading pipe file with tampon
        totBytes += nbrBytes;
        write(1, tmp, nbrBytes);
    }

    close(pipefd[0]); // Close read
    exit(0); // Close program
}
return 0;}

我只想用一个shell脚本作为参数来执行我的程序,程序从脚本中获取命令,然后它们由子进程执行,然后父进程显示结果^^

嗯,你试过argv[2],3,4…?也许我不明白你想做什么。。。如果我将您的程序编译为pg1,并按如下方式调用它:pg1 echo 123,则输出为--RESULT--123。。。所以pg1调用“echo”,参数为:1 2 3。这不是你想要的吗?@Mendo-你的程序已经使用一个shell脚本作为参数,只要它是可执行的,并且有一个适当的路径(如果需要)。我在尝试时出错…@TonyB这是我想要的,但我想要的不是pg1 echo 1 2 3,而是pg1 myscript。它执行myscript中的所有命令,并逐个显示结果