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命令的更多相关文章

  1. day20 二十、加密模块、操作配置文件、操作shell命令、xml模块

    一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...

  2. [蟒蛇菜谱] Python封装shell命令

    # -*- coding: utf-8 -*- import os import subprocess import signal import pwd import sys class MockLo ...

  3. 使用expect实现自动交互,shell命令行自动输入,脚本自动化,变量引用,expect spawn执行带引号命令,expect 变量为空,不生效,不能匹配通配符*,函数,数组

    背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...

  4. 使用expect实现自动交互,shell命令行自动输入

    背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...

  5. Linux下使用popen()执行shell命令【转】

    本文转载自:https://my.oschina.net/u/727148/blog/262987 函数原型: #include “stdio.h” FILE popen( const char co ...

  6. Linux下使用popen()执行shell命令

    转载 http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html 简单说一下popen()函数 函数定义 #include < ...

  7. Linux 操作基础(一) -- Shell 命令格式和元字符

    1 命令格式 cmd [-选项] [参数] 说明: • 最简单的Shell命令只有命令名,复杂的Shell命令可以有多个选项和参数 • 参数是文件也可以是目录,有些命令必须使用多个操作对象 • 并非所 ...

  8. hbase的常用的shell命令&hbase的DDL操作&hbase的DML操作

    前言 笔者在分类中的hbase栏目之前已经分享了hbase的安装以及一些常用的shell命令的使用,这里不仅仅重新复习一下shell命令,还会介绍hbase的DDL以及DML的相关操作. hbase的 ...

  9. Linux编程 22 shell编程(输出和输入重定向,管道,数学运算命令,退出脚本状态码)

    1. 输出重定向 最基本的重定向是将命令的输出发送到一个文件中.在bash shell中用大于号(>) ,格式如下:command > inputfile.例如:将date命令的输出内容, ...

随机推荐

  1. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  2. Yii源码阅读笔记(十五)

    Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...

  3. Bootstrap页面布局17 - BS选项卡

    代码结构: <div class='container-fluid'> <h2 class='page-header'>Bootstrap 选项卡</h2> < ...

  4. Ogre初入手:最简单的ogre程序骨架

    本文内容主要参考于页面 http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Ogre+Wiki+Tutorial+Framework Ogre是一个非 ...

  5. Laravel timestamps 设置为unix时间戳

    Laravel timestamps 设置为unix时间戳 class BaseModel extends Eloquent { /** * 默认使用时间戳戳功能 * * @var bool */ p ...

  6. 验证码识别 edge enhancement - 轮廓增强 region finding - 区域查找

    Computer Science An Overview _J. Glenn Brookshear _11th Edition The task of understanding general im ...

  7. sql_action

    CREATE TABLE w SELECT * FROM existing_table 2 日期x idm valuexm 日期x idn  valuexn 日期y idm  valueym 日期y ...

  8. SQL注入攻击和防御

    部分整理...   什么是SQL注入? 简单的例子, 对于一个购物网站,可以允许搜索,price小于某值的商品 这个值用户是可以输入的,比如,100 但是对于用户,如果输入,100' OR '1'=' ...

  9. Delphi 中的结构体与结构体指针

    好多程序都给结构体变量设定了一个结构体指针 例如: PAbc = ^TAbc; TAbc = record a: string[10]; b: string[5]; c: string[1]; end ...

  10. FTS抓包看L2CAP Connection的建立(一)

    一.概述     在前面的文章中介绍了inquiry和ACL connection的建立过程.这个连接建立后,L2CAP signaling channel(CID = 0x0001)就已经存在,可以 ...