【发布时间】:2023-05-23 08:37:01
【问题描述】:
我正在尝试使用 NSTask 执行此命令 ps -ef | grep test,但我无法获得 |将包含在 NSTask 中的 grep 测试:
这是我目前用来将 ps -ef 的输出转换为字符串然后我需要以某种方式获取进程测试的 pid
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ps"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-ef", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSLog (@"got\n%@", string);
【问题讨论】:
-
为什么不在程序中进行过滤呢?到 grep 的管道会更慢。
标签: objective-c cocoa