|
蓝森林 http://www.lslnet.com 2006年6月6日 10:18
c 中调用shell后如何获得shell的返回值?
高手们指点一下阿!万分感谢!!!! |
回复 1楼 bigsunflower 的帖子
看看函数的返回值!! |
是我表述不清楚,其实是这样的,我想在c程序中看一下系统有无xtjk_srv 这样的进程,然后再做下一步动作,
就是想在c程序中执行ps -ef|grep xtjk_srv 能把这个进程数传递给c程序就好了 |
#? 这个环境变量保存了返回值,读出来就是。 |
你用什么调的,system还是fork一个子进程exec?
system本身的返回值就可以. |
谢谢各位大侠,我想用system("ps -ef|grep xtjk_srv") 得到进程xtjk_srv的个数,判断它的个数然后写下面的程序 |
-->
ps -ef|grep xxxxx
这个写法不很严格 |
ps -ef | grep admind | grep -v 'grep' |
Popen |
int ret;
ret = system("ps -ef|grep xtjk_srv|grep -v grep") ;
ret = ( ret >> 8 );
printf("%d\n", ret); |
谢谢酷酷,我懂了 |
-->
仍然不严格 |
呵呵,小弟的方法到是能实现LZ想实现的功能, 只是有点烦琐,还请大家多指教:)
我要实现这样的功能一直用这样的方法,可能有点烦琐,还请大家多指点,程序写的很不规范,主要是说明方法:)
#include <stdlib.h>
#include <stdio.h>
int main()
{
int ret = 0;
char buf_line[100];
char buf_num[10];
FILE* fp;
system("ps -ef|grep httpd|wc -l > temp.dat");
fp = fopen("temp.dat", "r");
fgets(buf_line, 100, fp);
sscanf(buf_line, "%s", buf_num);
ret = atoi(buf_num);
printf("%d\n", ret);
return 0;
} |
用popen读进来,写一个看看,大家批评指正
FILE *fp;
char CmdLine[] = "ps -ef|grep xxxxx";
if((fp = popen(CmdLine, "r")) == NULL)
{
return false;
}
while((fgets(line, 512, fp)) != NULL)
{
......
} |
路过,呵呵 |
忏悔中!
看了楼上的兄弟有popen写的实现,感觉自己的实现太烂了,忏悔ing !!! |
| |