oc 调终端命令返回结果
2019-07-25 本文已影响0人
皮蛋豆腐酱油
- (NSString *)executeCommand: (NSString *)cmd {
NSString *output = [NSString string];
FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r+");
if (!pipe) {
return @"";
}
char buf[102400];
while(fgets(buf, sizeof(buf), pipe) != NULL) {
// if('\n' == buf[strlen(buf)-1]) {
// buf[strlen(buf)-1] = '\0';
// }
output = [output stringByAppendingFormat: @"%s ", buf];
}
pclose(pipe);
return output;
}
//这个也可以参考一下
FILE *fp = popen("ps -eo start,user,pid,pcpu,vsz,rss,etime,utime,stime,msgsnd,msgrcv", "r");