用管道:

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

  1. linux 8 -- 管道组合Shell命令进行系统管理

    二十. 通过管道组合Shell命令获取系统运行数据: 1.  输出当前系统中占用内存最多的5条命令:     #1) 通过ps命令列出当前主机正在运行的所有进程.     #2) 按照第五个字段基于数 ...

  2. linux重定向总结:如何将shell命令的输出信息自动输出到文件中保存

    在做批量实验室,例如跑批量MR的作业,我们会写好shell脚本,然后启动脚本,等所有作业执行完再去看结果,但是这些执行时的信息如何保存下来到文件中呢?下面这个命令可以完成这个任务. sh batchj ...

  3. Linux下Shell命令的输出信息同时显示在屏幕和保存到日志文件中

    #直接覆盖日志文件 ls -l | tee ./t.log #将输出内容附加到日志文件 ls -l | tee -a ./t.log 使用的是tee命令

  4. 获取标准shell 命令的输出内容

    cmdline.h #include <iostream> #include <mutex> class Cmdline { private: Cmdline() = defa ...

  5. c++ 使用shell命令

    #include <iostream> #include <stdio.h> #include <vector> #include <unistd.h> ...

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

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

  7. python 执行shell命令

    1.os模块中的os.system()这个函数来执行shell命令 1 2 3 >>> os.system('ls') anaconda-ks.cfg  install.log  i ...

  8. shell学习1---基本的shell命令

    基本脚本 反引号: ``  反引号里面的内容是命令行,通过反引号用户可以将shell命令的输出赋给变量,比如: test=`date +%y%m%d` echo "The time is : ...

  9. python2.7执行shell命令

    python学习——python中执行shell命令 2013-10-21 17:44:33 标签:python shell命令 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...

随机推荐

  1. Linux内存映射(mmap)系列(1)

    看到同事的代码中出现了mmap.所以自己私下学习学习,研究研究..... http://www.cnblogs.com/lknlfy/archive/2012/04/27/2473804.html ( ...

  2. C语言笔记(二维数组与数值指针)

    一.关于二维数组和二维数组区别 (1)一维数组在内存中是连续分布存储的,同样,二维数组也是在内存连续存储的.所以从内存的角度来分析,一维数组和二维数组其实没有本质区别. (2) 二维数组可以使用一维数 ...

  3. Warning: Invalid argument supplied for foreach()

    经常对提交过来的数据进行双重循环,但是为空时会报错:Warning: Invalid argument supplied for foreach() 如下解决即可:foreach($data[$i]  ...

  4. php mysql PDO使用

    <?php $dbh = new PDO('mysql:host=localhost;dbname=access_control', 'root', ''); $dbh->setAttri ...

  5. oracle技巧-持续更新

    1. 登录oracle数据库,执行select status from v$encryption_wallet,如果返回OPEN,表示钱夹已自动打开. 2.

  6. node案例

    http://www.cnblogs.com/wewe/archive/2010/03/19/1685658.html http://www.laonan.net/blog/69/ http://cn ...

  7. 框架中的HTML DOM Event 对象

    js中的this上下文会因事件而转换成html dom对象. 所以就有这样获取当前触发事件的dom对象: window.event.srcElement || window.event.target; ...

  8. codeforces C. Painting Fence

    http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...

  9. 创办支持多种屏幕尺寸的Android应用

    创建支持多种屏幕尺寸的Android应用 Android涉及各种各样的支持不同屏幕尺寸和密度的设备.对于应用程序,Android系统通过设备和句柄提供了统一的开发环境,大部分工作是校正每一个应用程序的 ...

  10. 【POJ】2886 Who Gets the Most Candies?

    移动题目相当麻烦. #include <stdio.h> #include <string.h> #define MAXN 500005 #define lson l, mid ...