Linux C语言获得执行的shell命令的返回值

Linux C语言获得执行的shell命令的返回值

#include <stdio.h>
#include <string.h>

int main()
{
    int cameraCount = 0;
    FILE *fstream=NULL;
    char buff[1024];
    char str[4096];
    memset(buff,0,sizeof(buff));
    // execute command get all video
    if(NULL==(fstream=popen("ls /dev/video*","r"))) 
    {
         //log_error("execute command %s failed", FIND_VIDEO_CMD); 
	 return -1;  
    } 
    // get video name
    memset(buff, 0x00, sizeof(buff));
    while(NULL!=fgets(buff, sizeof(buff), fstream)){
        log_info("video == %s", buff); 
        memset(buff, 0x00, sizeof(buff));
    }
    pclose(fstream);
    return 0; 
}

本文参考: linux C中调用shell命令和运行shell脚本

你可能感兴趣的:(Linux C语言获得执行的shell命令的返回值)