c execvp grep环境变量,当grep没有找到任何东西时

时间:2015-12-06 14:59:41

标签: c linux bash

我正在尝试构建一个研究环境变量的小型c程序。 当我使用参数运行程序时,它应该模仿

  

printenv | grep参数列表|排序|少

所以我使用管道连接所有这些输出,但现在问题是当我输入一个不存在的环境变量时,程序仍然运行所有内容,理想情况下我希望它在grep之后退出。

这是我用execvp和perror执行grep的代码,但是只有当execvp不能工作时才会出现perror。

我从手册页中知道,当选择了一行或多行时,grep给出0,当选择了无行时为1,当发生错误时,>为1。当grep选择没有行并退出程序而不继续排序和更少时,我如何处理?

    execvp( "grep", argv);  /* runs grep */
    perror( "Cannot exec grep" ); exit( 1 );




/* CHILD that handles grep function.
This child executes grep.

*/
if(pid == 0) 
{   
    if(-1 == dup2(pipa[READ],STDIN_FILENO)) /* redirect pipe1 to stdin */
    {perror("Cannot dup"); exit(EXIT_FAILURE);}

    if(-1 == dup2(pipa2[WRITE],STDOUT_FILENO)) /* redirect stdout to pipe2 */
    {perror("Cannot dup"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa[WRITE])) /* Close write, we dont need it */
    {perror("Cannot close pipe (write-end)[GREP]"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa[READ])) /* Close read on pipe1 */
    {perror("Cannot close pipe (read-end)[GREP]"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa2[READ])) /* Close read on pipe2, we dont need it */
    {perror("Cannot close pipe2 (read-end)[GREP]"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa2[WRITE])) /* Close write on pipe2, we we have it on stdout now */
    {perror("Cannot close pipe2 (read-end)[GREP]"); exit(EXIT_FAILURE);}

    if(argc > 1) /* filter or no */
    {
        argv[0] = "grep";
        printf("%s",argv[0]);

        execvp( "grep", argv);  /* runs grep */
        perror( "Cannot exec grep" ); exit( 1 );
    }
    exit(0);
}

/* PARENT */
if(argc < 2) /* No arguments? */
{
    if(-1 == dup2(pipa2[WRITE],pipa[WRITE])) /* Redirect stdin to pipe2 write (which is in STDOUT_FILENO) */
    {perror("Cannot dup"); exit(EXIT_FAILURE);}
}


/* Sends enivomrent data to grep */
for(i=0; envp[i] != 0; i++)
{
    write(pipa[WRITE],envp[i],strlen(envp[i]));
    write(pipa[WRITE],"\n",1);
}

nchildren++;

/* Create 3rd pipe */
if(pipe(pipa3) == -1)
{exit(EXIT_FAILURE);}

/* Close 1st pipe */
if(-1 == close(pipa[WRITE]))
{perror("Cannot close pipe (write-end)"); exit(EXIT_FAILURE);}
if(-1 == close(pipa[READ]))
{perror("Cannot close pipe (read-end)"); exit(EXIT_FAILURE);}
/* Close write from second pipe */
if(-1 == close(pipa2[WRITE]))
{perror("Cannot close pipe (read-end)"); exit(EXIT_FAILURE);}


/* fork again */
if((pid = fork()) == -1) /* ERROR */
{exit(EXIT_FAILURE);}

/* CHILD that handles sort function.
This child executes sort.

*/
if(pid == 0) 
{   
    if(-1 == dup2(pipa2[READ],STDIN_FILENO)) /* redirect pipe2 read to stdin */
    {perror("Cannot dup"); exit(EXIT_FAILURE);}

    if(-1 == dup2(pipa3[WRITE],STDOUT_FILENO)) /* redirect stdout to pipe3 write */
    {perror("Cannot dup"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa2[READ])) /* Close write, we have it on stdin now */
    {perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa3[READ])) /* Close read on pipe3, we dont need it */
    {perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}

    if(-1 == close(pipa3[WRITE])) /* Close write on pipe3, we have it on stdout now*/
    {perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}

    (void) execlp( "sort", "", NULL );  /* runs sort */
    perror( "Cannot exec sort" ); exit( 1 );
}

/* PARENT */
if(-1 == close(pipa2[READ])) /* Close read from pipe2 */
{perror("Cannot close pipe (read-end)[PARENT]"); exit(1);}
if(-1 == close(pipa3[WRITE])) /* Close write from pipe3*/
{perror("Cannot close pipe (write-end)[PARENT]"); exit(1);}

1 个答案:

答案 0 :(得分:1)

如果execvp失败,将调用_exit。

execvp的手册页:

返回值 如果任何exec()系列函数返回,则会发生错误。返回值为-1,将设置全局变量errno以指示错误。