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);