我想執行這個comamnd ps -ef | grep test
使用NSTask,但我不能得到|被列入NSTask的grep測試:使用|執行shell命令(管道)使用NSTask
這是我使用的是什麼目前得到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會更慢。 – 2011-09-10 12:52:25