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命令的输出内容, ...
随机推荐
- data structure | heap
#include <iostream> #include <string.h> using namespace std; template <class T> cl ...
- CSS3:渐变大全
渐变大全 声明 最后的老写法镜像渐变可能不太准确.其余都完全正确 <!DOCTYPE html> <html> <head> <meta http-equiv ...
- 每天学点GDB 13
ptrace是gdb实现的基石,本文简要介绍一下ptrace. ptrace linux提供的系统调用ptrace,使得一个进程可以attach到另一个进程并进而完整的控制被attach上的进程. 被 ...
- DS实验题 地鼠安家
★实验任务 fd是一个公认的美丽校园.一天,fd来了一群地鼠,编号为1到n,他们希望在这里定居.现在先由第一只地鼠往下打一个单位的距离,并且在那里安家.对于每一个已经安家的地鼠,如果他左下或右下没有邻 ...
- Ubuntu下搭建NodeJS+Express WEB开发框架
Ubuntu下搭建NodeJS+Express WEB开发框架 2012-12-27 15:06 作者: NodeJSNet 来源: 本站 浏览: 2,966 次阅读 我要评论暂无评论 字号: 大 中 ...
- PDB文件:每个开发人员都必须知道的
PDB Files: What Every Developer Must Knowhttp://www.wintellect.com/CS/blogs/jrobbins/archive/2009/05 ...
- laravel ajax表格删除
view和jq @extends('layouts.main') @section('content') <h3>User List</h3> <p class=&quo ...
- 微信公众账号开发教程(四)自定义菜单(含实例源码)——转自http://www.cnblogs.com/yank/p/3418194.html
微信公众账号开发教程(四)自定义菜单 请尊重作者版权,如需转载,请标明出处. 应大家强烈要求,将自定义菜单功能课程提前. 一.概述: 如果只有输入框,可能太简单,感觉像命令行.自定义菜单,给我们提供了 ...
- 纯css下拉菜单的制作
通过观察下拉菜单的过程,发现有两种状态,一种是鼠标没有hover时,一种是鼠标hover时. 主菜单hover时,显示子菜单:主菜单没有hover时,不显示子菜单 <!DOCTYPE html& ...
- Android TextView 显示不全的自动补齐方式
TextView在Android开发中用到的地方应该是很多的.很多时候,TextView会有一行显示不全被截取或者会换行.之前我的解决办法比较笨拙,定死TextView的一行字数长度,最后一个以省略号 ...