Skip to content

Instantly share code, notes, and snippets.

@skahack
Created April 14, 2011 05:11
Show Gist options
  • Save skahack/918925 to your computer and use it in GitHub Desktop.
Save skahack/918925 to your computer and use it in GitHub Desktop.
exec system command
- (NSString *)runCommand:(NSString *)command {
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
[task setLaunchPath: @"/bin/sh"];
[task setArguments: [NSArray arrayWithObjects:@"-c", command, nil]];
[task setStandardOutput: pipe];
[task launch];
NSFileHandle *handle = [pipe fileHandleForReading];
NSData *data = [handle readDataToEndOfFile];
NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[pipe release];
[task release];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment