An example from modifying some of my code...
This uses /bin/sh -c to execute the command, which treats the string as if it was typed onto the command line. You may very well find it easier to just set the launchPath and arguments directly to what you want to run.
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSArray *LinesByExecutingCommand(NSString *command)
{
NSPipe *pipe = [[NSPipe alloc] init];
NSTask *task = [[NSTask alloc] init];
NSData *output;
NSString *stringOutput;
[task setLaunchPath:<font color = orange>@"/bin/sh"</font>];
[task setArguments:[NSArray arrayWithObjects:<font color = orange>@"-c"</font>, command, nil]];
[task setStandardOutput
ipe];
[task launch];
output = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[pipe release];
[task release];
<font color = brown>//[NSString stringWithCString:[output bytes] length:[output length]]</font>
<font color = brown>//would also work, but with the following you can put in a different</font>
<font color = brown>//string encoding if you know any better what the output will be</font>
<font color = brown>//(it'll be NSMacOSRomanStringEncoding by default).</font>
stringOutput = [[[NSString alloc] initWithData:output encoding:[NSString defaultCStringEncoding]] autorelease];
return [stringOutput componentsSeparatedByString:<font color = orange>@"\n"</font>];
}
</font>[/code]