有什么好的免费网站做教育宣传,网站模板 seo,中国域名后缀,自己做都网站怎么发朋友圈如果在C程序中调用了shell命令#xff0c;那么往往希望得到输出结果以及命令执行的返回布尔值。在这里分为两步来处理#xff1a; 1.使用 popen 与 pclose 来执行shell命令#xff1b; 2.使用‘echo $?’来获取上一条指令执行状态#xff0c;如果为0那么标识成功执行那么往往希望得到输出结果以及命令执行的返回布尔值。在这里分为两步来处理 1.使用 popen 与 pclose 来执行shell命令 2.使用‘echo $?’来获取上一条指令执行状态如果为0那么标识成功执行否则标识执行出错
代码如下
#include stdio.h
#include stdlib.h
#include string.h
#include unistd.h
#include errno.hint main(void)
{FILE *stream NULL;char buf[1024];int ret;memset(buf, 0, sizeof(buf));if ((stream popen(ifconfig, r)) NULL) {fprintf(stderr, %s, strerror(errno));return -1;/* output the message */while (fgets(buf, sizeof(buf), stream) ! NULL) {printf(%s, buf);}if ((stream popen(echo $?, r)) NULL) {fprintf(stderr, %s, strerror(errno));return -1;/* output the message */while (fgets(buf, sizeof(buf), stream) ! NULL) {printf(%s, buf);}ret atoi(buf);if (ret)printf(command excutes succeed!\n);else printf(command excutes fail!\n);
}