您的位置:首页 > 运维架构 > Shell

c函数中执行shell 命令

2011-12-09 01:03 197 查看
在c函数中执行shell 命令 并获得命令的返回值

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int ret = -1;
FILE* pf;
/* Can not get the output of the cmd */
ret = system("ls /dev/loop* | wc -l");

printf("ret = %d\n", ret);

/* This mothed can get the output of the cmd */
pf  = popen("ls /dev/loop* | wc -l", "r");

fscanf(pf, "%d", &ret);
printf("ret = %d\n", ret);

pclose(pf);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: