[转] 用管道获得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命令 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...
随机推荐
- nRF52系列——Get started
Install Jlink Install MDK 这里的使用的是MDK 5.14 Install SDK https://developer.nordicsemi.com/nRF52_SDK/ 这里 ...
- JDK源码阅读(二) AbstractList
package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> i ...
- C++中四种类型转换方式
类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...
- Java操作Oracle数据库以及调用存储过程
操作Oracle数据库 publicclass DBConnection { //jdbc:oracle:thin:@localhost:1521:orcl publicstaticf ...
- http server v0.1_http_server.c
/**************************************************************** filename: http_server.c author: xx ...
- 你真的了解 MySQL 数据库的运行状况吗?
2015年第三方市场调查机构 Evans 数据公司最近公布的一系列客户调查数据显示,在过去两年里,MySQL 在所有开发者使用的数据库中获得了25%的市场份额,Evans 公司的本次调查显示,数据库的 ...
- andorid studio
http://www.cnblogs.com/smyhvae/p/4390905.html
- 【POJ11855】 Buzzwords (后缀数组)
Description The word “the” is the most commonthree-letter word. It evenshows up inside other words, ...
- Hibernate Is Not Mapped(实体名 is not mapped [from book where id='0'])
org.springframework.orm.hibernate3.HibernateQueryException: USERINFO is not mapped.看到.hbm.xml文件中的< ...
- Delphi如何让程序最小化到任务栏(截取WM_SYSCOMMAND后,调用Shell_NotifyIcon)
现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供了这样的功 ...