0

当脚本“ check.sh ”没有返回任何内容时,我需要在我的窗口中打印一些内容,这意味着当脚本没有返回任何输出时进行验证。

check.sh 不包含任何内容。它只是一个空白的 sh 文件,执行时不返回任何内容。我正在使用一个空的 sh 文件进行测试(我无法向您展示确切的脚本,这就是原因)。

当 check.sh 什么都不返回时,我想打印一些消息,例如通过 C 进行“配置某些东西”。

我用 "\n","\r","\0",NULL. 检查了缓冲区行(检查下面的模块)。我不知道脚本什么都不返回时会发生什么

我将把模块称为execute_command("sh check.sh")

这是我的模块

char *execute_command(char *command)
{
    FILE *fpipe;
    char line[1024]="";
    //char *line = (char*)malloc(1024*sizeof(char));
    int i =0;

    if ( !(fpipe = (FILE*)popen(command,"r")) )
    {  // If fpipe is NULL
        perror("Problems with pipe");
        exit(1);
    }

    while ( fgets( line, sizeof line, fpipe))
    {
        // printf("%s", line);
    }

    while(line[i]!='\0')
    {
        if(line[i]==' ')
        {
            line[i]=',';
        }
        i++;
    }
    pclose(fpipe);
    printf("%s",line); // This is where i want to know what the buffer has when the script returns nothing 
    return(line);
}
4

1 回答 1

0

根据这个fgets手册页,如果在读取任何字符之前发生文件结束,它会返回NULL并且缓冲区内容保持不变。

于 2010-02-03T15:09:36.350 回答