捕获从命令行返回的空输出,如果为空则显示一些消息
当脚本“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);
}
I need to print something in my window when the script "check.sh" returns nothing, means validation when no output from the script is returned.
check.sh contains nothing in it. It is simply a blank sh file that returns nothing when executed. I am testing with an empty sh file (I cannot show u the exact script that is why).
What I want to print is some message like "configure some thing" through C when check.sh returns nothing.
I checked buffer line(check in the module below) with "\n","\r","\0",NULL.. I don't know what it is taking when the script returns nothing
I'll be calling the module as execute_command("sh check.sh")
Here is my module
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据此 fgets 手册页,如果结束 - of-file 在读取任何字符之前发生,它返回 NULL 且缓冲区内容保持不变。
According to this fgets man page, if end-of-file occurs before any characters are read, it returns
NULL
and the buffer contents remain unchanged.