[转] 用管道获得shell 命令的输出
用管道:
通过fgets(buf, n, ptr)buf就可以得到命令“ps -ef"一样的信息,
读帮助”man popen":
char *cmd = "ps -ef";
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, n, ptr) != NULL)
(void) printf("%s ",buf);
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 Sep-30 ? 00:00:01 sched
root 1 0 0 Sep-30 ? 00:00:06 /etc/init -a
root 2 0 0 Sep-30 ? 00:00:00 vhand
root 3 0 0 Sep-30 ? 00:00:27 bdflush
root 4 0 0 Sep-30 ? 00:00:00 kmdaemon
root 5 1 0 Sep-30 ? 00:00:50 htepi_daemon /
root 6 0 0 Sep-30 ? 00:00:00 strd
root 2941 1 0 Oct-08 tty01 00:00:00 /bin/login ccb
root 43 1 0 Oct-08 ? 00:00:02 /etc/syslogd
root 47 1 0 Oct-08 ? 00:00:00 /etc/ifor_pmd
root 48 47 0 Oct-08 ? 00:00:13 /etc/ifor_pmd
root 36 1 0 Oct-08 ? 00:00:00
[转] 用管道获得shell 命令的输出的更多相关文章
- linux 8 -- 管道组合Shell命令进行系统管理
二十. 通过管道组合Shell命令获取系统运行数据: 1. 输出当前系统中占用内存最多的5条命令: #1) 通过ps命令列出当前主机正在运行的所有进程. #2) 按照第五个字段基于数 ...
- linux重定向总结:如何将shell命令的输出信息自动输出到文件中保存
在做批量实验室,例如跑批量MR的作业,我们会写好shell脚本,然后启动脚本,等所有作业执行完再去看结果,但是这些执行时的信息如何保存下来到文件中呢?下面这个命令可以完成这个任务. sh batchj ...
- Linux下Shell命令的输出信息同时显示在屏幕和保存到日志文件中
#直接覆盖日志文件 ls -l | tee ./t.log #将输出内容附加到日志文件 ls -l | tee -a ./t.log 使用的是tee命令
- 获取标准shell 命令的输出内容
cmdline.h #include <iostream> #include <mutex> class Cmdline { private: Cmdline() = defa ...
- c++ 使用shell命令
#include <iostream> #include <stdio.h> #include <vector> #include <unistd.h> ...
- Linux下使用popen()执行shell命令
转载 http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html 简单说一下popen()函数 函数定义 #include < ...
- python 执行shell命令
1.os模块中的os.system()这个函数来执行shell命令 1 2 3 >>> os.system('ls') anaconda-ks.cfg install.log i ...
- shell学习1---基本的shell命令
基本脚本 反引号: `` 反引号里面的内容是命令行,通过反引号用户可以将shell命令的输出赋给变量,比如: test=`date +%y%m%d` echo "The time is : ...
- python2.7执行shell命令
python学习——python中执行shell命令 2013-10-21 17:44:33 标签:python shell命令 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...
随机推荐
- sourceInsight的技巧
在用sourceInsight看代码...在这里积累技巧,慢慢积累吧 1.如何高亮显示所有要搜的东西,例如 1.aaaaaa 2. bbbbbbbbaaaaaaa 3. ccccccc 4. aaaa ...
- windows通过Composer安装yii2
1. php.ini 中;extension=php_openssl.dll(取消注释,不然在安装composer过程中会报错) 集成环境最好去php目录中打开php.ini文件,确定;extensi ...
- make xxx Is a directory. Stop.
转自:make xxx Is a directory. Stop. 的原因 make xxx Is a directory. Stop. 的原因 编译内核时候的一个错误提示 make: *** ...
- Noah的学习笔记之Python篇:装饰器
Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) ...
- css3 3D盒子效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Git遇到的一点错误
[背景] 折腾: [记录]将googlecode上面的crifanLib迁移到Github上 期间出错: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- c++复习基础要点02 虚函数与模板 与static inline是否共存
1. 虚函数能否定义为模板函数 当一个类有虚函数时,它一定有一个虚表,用来纪录每个虚函数的实际地址.这也就是说这个虚表的大小是在编译期就确定了的.有多少个虚函数,虚表就纪录几个. ...
- 等方案及设备提供商 有需要的可以联系QQ561454825,电话:13779953060,我们提供最专业的无线WIFI认证系统及根据您的需要修改软件
WayOs智能路由.EasyRadius云计费.POE远程供电.WIFI城中村方案.EPON实现FTTB+LAN城中村方案. 等方案及设备提供商 有需要的可以联系QQ561454825,电话:,我们提 ...
- Hadoop 新 MapReduce 框架 Yarn 详解
Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...
- 【转】怎么在Foxmail回复/转发时使用签名?
原文网址:http://kf.qq.com/faq/120322fu63YV130422yABZRZ.html Foxmail回复/转发时使用签名,可通过在模版中设置签名.如下版本操作方法: 一.fo ...