I've got an NSTask that uses a piped grep command; The output matches, and grep comes back with a match to my input string — although when comparing it back to the original string it doesn't match somehow. I'm probably not comparing the returned string properly against the other one? please help.
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];
It fails with the output in console looking like this:
grep returned:
words
grep searched:
words
failed