popen&pclose管道方式操作shell命令

时间:2022-03-21 08:13:33

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 &lt ...

  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. aspx页面,中文乱码解决方案

    由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...

  2. Android.mk 基本应用

    如果是在android源码里面编译我们自己的应用,就需要这个android.mk文件,这个文件就告诉android系统应用如何来编译这个应用以及这个应用它所依赖哪些文件等等信息.我对android.m ...

  3. 打开别人Xamarin项目找不到android.jar文件

    打开别人Xamarin项目找不到android.jar文件 错误信息:Could not find android.jar for API Level 23.打开非本机创建的Xamarin项目,编译的 ...

  4. [再寄小读者之数学篇](2014-11-26 广义 Schur 分解定理)

    设 $A,B\in \bbR^{n\times n}$ 的特征值都是实数, 则存在正交阵 $P,Q$ 使得 $PAQ$, $PBQ$ 为上三角阵.

  5. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  6. 述 SQL 中的 distinct 和 row_number() over() 的区别及用法

    1 前言 在咱们编写 SQL 语句操作数据库中的数据的时候,有可能会遇到一些不太爽的问题,例如对于同一字段拥有相同名称的记录,我们只需要显示一条,但实际上数据库中可能含有多条拥有相同名称的记录,从而在 ...

  7. cout快捷转换进制

    cout<<hex<<i<<endl; //输出十六进制数 cout<<oct<<i<<endl; //输出八进制数 cout& ...

  8. spring的定时任务配置(注解)

    参考博客: http://www.jb51.net/article/110541.htm http://blog.csdn.net/wxwzy738/article/details/25158787 ...

  9. hadoop job -kill 和 yarn application -kill 区别

    hadoop job -kill 调用的是CLI.java里面的job.killJob(); 这里会分几种情况,如果是能查询到状态是RUNNING的话,是直接向AppMaster发送kill请求的.Y ...

  10. 洛谷P2119 魔法阵

    P2119 魔法阵 题目描述 六十年一次的魔法战争就要开始了,大魔法师准备从附近的魔法场中汲取魔法能量. 大魔法师有m个魔法物品,编号分别为1,2,...,m.每个物品具有一个魔法值,我们用Xi表示编 ...