我有一个使用管道 grep 命令的 NSTask;输出匹配,并且 grep 与我的输入字符串匹配 - 尽管将其与原始字符串进行比较时,它以某种方式不匹配。我可能没有正确地将返回的字符串与另一个字符串进行比较?请帮忙。
NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/path/to/file"];
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/grep"];
NSString *words = @"words";
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-o", "-a", "-m 1", @"-h", @"-r", words, filePath, 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 (@"grep returned:\n%@", string);
NSLog (@"grep searched:\n%@", words);
[task release];
if ([string isEqualToString:words]) {
NSLog(@"match: %@", string);
} else {
NSLog(@"failed");
[string release];
它失败,控制台中的输出如下所示:
grep returned:
words
grep searched:
words
failed