Linux程序中执行shell(程序、脚本)并获得输出结果
来源:互联网 发布:2016年度网络广告公司 编辑:程序博客网 时间:2024/05/17 06:31
系统函数popen函数可以处理调用shell,其函数原型如下:
FILE *popen(const char * command,const char *type);
该函数的作用是创建一个管道,fork一个进程,然后执行shell,而shell的输出可以采用读取文件的方式获得。采用这种方法,既可以避免了创建临时文件,又不受输出字符的限制,推荐使用。
popen使用FIFO管道执行外部程序。
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的 stdout, popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#define BUFLEN (1024)int readconsole(char *cmd, char *buf, int buflen) { FILE *stream; int readlen; stream = popen(cmd, "r" ); readlen = fread( buf, sizeof(char), buflen, stream); pclose( stream ); return readlen;}int main( void ){ char buf[BUFLEN]; int readlen; memset( buf, '\0', BUFLEN ); readlen = readconsole("/media/workspace/vincent/develop/app/exec/my_shell", buf, BUFLEN); printf("myshell len=%d,buf=%s\n", readlen, buf); return 0;}
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- 从Linux程序中执行shell(程序、脚本)并获得输出结果
- linux 或WIN 程序中执行脚本获得输出结果
- Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量)
- c程序中获得命令行输出结果
- linux的C程序 调用 shell脚本,获取shell的执行结果
- 50款经典 iPhone 开源应用(附代码下载)
- Flash Socket通信的安全策略问题 843端口
- linux下vim命令详解
- nagios插件之计算asterisk当前通话量
- office__Ribbon界面的设计
- Linux程序中执行shell(程序、脚本)并获得输出结果
- mysql 语句大全
- preference的使用
- 和菜鸟一起学linux之bluez学习记录1
- stk-matlab 交互命令大全
- hibernate lockMode(乐观锁和悲观锁)
- 用win7自带IIS7.0发布ISAPI模块
- 微软中国反盗 20 年,人不服水土
- 视频与图像RGB/YUV格式详解 (2012-04-24 15:21:16)