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命令的输出内容, ...
随机推荐
- [转]C#基础回顾:Asp.net 缓存
本文转自http://www.cnblogs.com/stg609/archive/2009/03/22/1418992.html 缓存的作用 你买电脑的时候,是否会在意CPU的二级缓存?是 ...
- elasticsearch插件大全
Elasticsearch扩展性非常好,有很多官方和第三方开发的插件,下面以分词.同步.数据传输.脚本支持.站点.其它这几个类别进行划分. 分词插件 Combo Analysis Plugin (作者 ...
- CSS系列:表达式(Expression)`淘汰`
概述 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式. 兼容性 expression方法在其它浏览器中不起作用,因此在跨浏览器 ...
- hlg 2130 状压dp
基本的状压dp 需要注意的是两点之间直线最短 所以不需要进行floyd 由于把dp的memset放在了初始化0的后面de了好久的bug.. #include<stdio.h> #inclu ...
- A trip through the Graphics Pipeline 2011_04
Welcome back. Last part was about vertex shaders, with some coverage of GPU shader units in general. ...
- [ZZ] D3D中的模板缓存(3)
http://www.cppblog.com/lovedday/archive/2008/03/25/45334.html http://www.cppblog.com/lovedday/ D3D中的 ...
- Frenetic HelloSDNWorld
Follow Frenetic-Github HelloSDNWorld 实验环境: Frenetic虚拟机: 实验步骤: 1.Start up a terminal window - – two a ...
- thinkphp四种url访问方式详解
本文实例分析了thinkphp的四种url访问方式.分享给大家供大家参考.具体分析如下: 一.什么是MVC thinkphp的MVC模式非常灵活,即使只有三个中和一个也可以运行. M -Model 编 ...
- Delphi指针及其它(转)
一.指针:指向一个内存地址的变量或参数. 二.定义指针的方式如下: P: Pointer; //定义了可以指向任何类型的指针,Pointer 为无类型指针: Q, R: ^TType; //定义了指向 ...
- php 通过exec 创建git分支失败
今天给我们自己的发布系统增加一个新建分支的功能,操作比较简单,但是使用php执行shell命令的时候总是无法push分支到远程,但是登陆服务器执行却是可以的 新建分支命令如下 git fetch -- ...