[转] 用管道获得shell 命令的输出
2015-12-11 07:32
489 查看
用管道:
通过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
通过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
相关文章推荐
- sql server 开xp_cmdhshell
- shell学习笔记
- BeanShell语法学习提要
- grep指定文件中的所有内容
- Shell学习--(3)替换,字符串,数组
- powershell脚本-DNS
- shell变量数学运算
- Shell学习--(2)变量
- Shell学习--(1)
- Bash环境配置文件
- shell学习笔记
- shell中“>/dev/null 2>&1”详解
- 关于bash中的数据流重导向
- 关于bash中的数据流重导向
- nodejs 执行shell 命令
- Linux下Shell发送附件邮件&中文乱码
- shell之logger
- shell之$(cmd)
- shell之cut
- shell之test