popen&pclose管道方式操作shell命令
popen, pclose - pipe stream to or from a process
FILE *popen( const char *command, const char *type);
int pclose(FILE *stream);
描述
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The type argument is a pointer to a null-terminated string whichi must contain either the letter 'r' for reading or the lette 'w' for writing. Since glibc 2.9, this argument can additionally include the letter 'e', whichi causes the close-on-exec flag(FD_CLOEXEC) to be set on the underlying file descriptor; see the description of the O_CLOEXEC flag in open for resons why this may be usefull.
The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that call popen(), unless this is altered by the command itself. Conversely, reading from a "popened" stream reads the command's standard output, and the command's standard input is the same as that of the process the called popen().
Note that output popen() streams are fully buffered by default.
The pclose() function waits for the associated process to terminate and returns the exit status of the comman as returned by wait4.
返回值
The popen() function returns NULL if the fork or pipe calls fail, or if it cannot allocate memory.
The pclose() function returns -1 if wait4 returns an error, or some other error is detected. In the event of an error, these functions set errno to indicate the cause of the error.
BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen().
封装函数
- int popen_get_string(char *cmd, char *buf, int buf_size)
- {
- if(!buf || !buf_size){
- return -;
- }
- FILE *fp = popen(cmd, "r");
- if(!fp){
- return -;
- }
- if(fgets(buf, buf_size, fp) == NULL){
- pclose(fp);
- return -;
- }
- pclose(fp);
- int len = strlen(buf);
- if(len > && buf[len-] == '\n'){
- buf[--len] = ;
- }
- return len;
- }
另一示例
- int check_net(const char *eth)
- {
- int ret = ;
- char buf[];
- FILE *fp;
- memset(buf, , );
- sprintf(buf, "ifconfig %s | grep 'RUNNING'", eth);
- fp = popen(buf, "r");
- if(fp == NULL)
- {
- printf("failed to popen %s\n", buf);
- return ;
- }
- memset(buf, , );
- fgets(buf, , fp);
- if(!strcmp(buf, ""))
- ret = ;
- else
- ret = ;
- pclose(fp);
- }
popen&pclose管道方式操作shell命令的更多相关文章
- day20 二十、加密模块、操作配置文件、操作shell命令、xml模块
一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...
- [蟒蛇菜谱] Python封装shell命令
# -*- coding: utf-8 -*- import os import subprocess import signal import pwd import sys class MockLo ...
- 使用expect实现自动交互,shell命令行自动输入,脚本自动化,变量引用,expect spawn执行带引号命令,expect 变量为空,不生效,不能匹配通配符*,函数,数组
背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...
- 使用expect实现自动交互,shell命令行自动输入
背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...
- Linux下使用popen()执行shell命令【转】
本文转载自:https://my.oschina.net/u/727148/blog/262987 函数原型: #include “stdio.h” FILE popen( const char co ...
- Linux下使用popen()执行shell命令
转载 http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html 简单说一下popen()函数 函数定义 #include < ...
- Linux 操作基础(一) -- Shell 命令格式和元字符
1 命令格式 cmd [-选项] [参数] 说明: • 最简单的Shell命令只有命令名,复杂的Shell命令可以有多个选项和参数 • 参数是文件也可以是目录,有些命令必须使用多个操作对象 • 并非所 ...
- hbase的常用的shell命令&hbase的DDL操作&hbase的DML操作
前言 笔者在分类中的hbase栏目之前已经分享了hbase的安装以及一些常用的shell命令的使用,这里不仅仅重新复习一下shell命令,还会介绍hbase的DDL以及DML的相关操作. hbase的 ...
- Linux编程 22 shell编程(输出和输入重定向,管道,数学运算命令,退出脚本状态码)
1. 输出重定向 最基本的重定向是将命令的输出发送到一个文件中.在bash shell中用大于号(>) ,格式如下:command > inputfile.例如:将date命令的输出内容, ...
随机推荐
- elFinder的使用并整合ckeditor
具体实现访问: 白乔大神资源:https://github.com/bluejoe2008/elfinder-2.x-servlet 本人oschina的库:https://git.oschina.n ...
- Key-Vlaue Coding Apple官方翻译
今天是键值编码,网上有很多文章,可以百度.不太理解的就看官方文档吧 键-值编码 键值编码是一种运用字符串标识符来间接访问一个对象的属性和关系的机制.它尤其强化并关联了多种Cocoa编程的机制和技术,体 ...
- java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim [问题点数:40分,结帖人wangxiaohua_001]
14:56:10.093 WARN!! Error for /butterfly/plugins/zhonghang/UsefulData/save_usefuldata.bshjava.lang.N ...
- 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3
一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...
- service mongod start start: Unknown job: mongod问题
终于解决了这个异常蛋疼的问题,当安装完毕mongodb的时候,执行: root@ubuntu:/usr/local# service mongod start 出现: start: Unknown j ...
- json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。
json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...
- java-冒泡排序
1.打印 print--打印,不换行,根据要求加上换行符 println--打印一次就换行 printf--打印,继承C语音的格式,可以进行格式化输出 换行符 '\r'是回车,'\n'是换行,‘\t' ...
- Python之 continue继续循环和多重循环
Python之 continue继续循环 在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环. 假设我们已经写好了利用for循环计算平均分的代码: L ...
- JAVA缓存技术之EhCache
最近再ITEYE上看到关于讨论JAVA缓存技术的帖子比较多,自己不懂,所以上网大概搜了下,找到一篇,暂作保存,后面如果有用到可以参考.此为转贴,帖子来处:http://cogipard.info/ar ...
- node.js express的安装过程
1.配置nodejs的环境变量之后执行 npm install -g express 命令: 2.如果版本为4.x需要再次执行 npm install -g express-generato ...