当脚本“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);
}
最佳答案
根据此 fgets 手册页中,如果在读取任何字符之前发生文件结束,则返回 NULL 且缓冲区内容保持不变。
关于捕获从命令行返回的空输出,如果为空则显示一些消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2190660/