objective-cshellcommandnstasknspipe

Strange issue of output via NSPipe when execute shell command using NSTask


Here is my codes. When I set myCmd=@"cd /\nls -l\n" or myCmd=@"ls -l\n", it's no problem. However, when I set myCmd=@"cd /\n", program is dead in the line if ((output =[[outPipe fileHandleForReading] availableData]) && [output length]) and there is nothing debug info output.

I don't know if the "cd /" cmd is different with other shell command. Could you give me some advice?

NSData *inputData = [myCmd dataUsingEncoding:NSUTF8StringEncoding];
NSPipe *inPipe = [NSPipe pipe];
NSFileHandle *fh = [inPipe fileHandleForWriting];
[fh writeData: inputData];
NSPipe *outPipe = [NSPipe pipe];
//NSPipe *errPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setStandardInput:inPipe];
[task setStandardOutput:outPipe];
[task setStandardError:outPipe];
[task setLaunchPath:@"/bin/sh"];
NSArray *args = [NSArray arrayWithObject:@"-s"];
[task setArguments:args];

[task launch];

NSData *output;
NSString *string;

if ((output =[[outPipe fileHandleForReading] availableData]) && [output length]) 
{
    string = [[NSString alloc] initWithFormat:@"%.s", [output bytes]];
}
NSLog(@"%@", string);

Solution

  • I don't know if the cd / cmd is different with other shell command.

    It's different fromm ls -l in that it doesn't write any output. Your program is probably blocked in the call to -availableData.


    Unfortunately, I don't have time to try out any ideas but here are some things you might try.

    I should point out, by the way, that sending cd to a shell as the last thing you do is a pointless operation. The next thing that happens is that the shell exits and the cd results are lost. If your objective is to change the directory in the current process, look at [NSFilemanager changeCurrentDirectoryPath:]