zoukankan      html  css  js  c++  java
  • [转] 用管道获得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

  • 相关阅读:
    从多渠道打包与友盟统计有这一篇就够了
    多渠道打包
    studio构建错误Element uses-permission#android.permission.ACCESS_NETWORK_STATE at AndroidManifest.xml:38:5-79 dupli
    ADB命令与monkey
    正则表达式和文本挖掘(Text Mining)
    一步一步教你使用Git
    Android常见开源解决方案
    Android Intent到底能做些什么
    支付宝集成
    Theano 学习三 conv2d
  • 原文地址:https://www.cnblogs.com/qiangxia/p/5037902.html
Copyright © 2011-2022 走看看