【发布时间】:2012-05-16 15:04:58
【问题描述】:
我有如下代码,没关系,但我有两个问题。
1) 如果我为 sh 分配了一些非法参数,例如@"ls - l",那么 outString 为空。也就是无法捕捉到错误警告“ls: -: No such file or directory ls:l:No such file or directory”。如何处理?
2)如何实现这个功能:假设应用的当前目录是"/user/Doc",我执行sh = @"cd /",然后我执行 sh = @"ls -l" 来查看“/”下的内容 下一个循环的目录。但是当新循环开始时,当前目录恢复到“/user/Doc”。怎样才能保持上次循环的任务环境?
此外,我可以像直接在终端上工作一样,继续运行“/bin/sh”吗?
NSString *sh = @"ls -l";
while(sh != @"end"){
NSTask *shData = [[NSTask alloc] init];
[shData setLaunchPath: @"/bin/sh"];
NSArray *args;
args = [NSArray arrayWithObjects: @"-c", sh, nil];
[shData setArguments: args];
NSPipe *myPipe;
myPipe = [NSPipe pipe];
[shData setStandardOutput: myPipe];
[shData setStandardInput:[NSPipe pipe]];
NSFileHandle *file;
file = [myPipe fileHandleForReading];
[shData launch];
NSData *data1;
data1 = [file readDataToEndOfFile];
NSString *outString;
outString = [[NSString alloc] initWithData: data1 encoding: NSUTF8StringEncoding];
NSLog(@"%@",outString);
}
【问题讨论】:
-
对于工作目录,为什么不直接使用
-[NSTask setCurrentDirectoryPath:]? -
这不是我需要的。我的目的是使用不同的参数连续运行任务,但要求下一次运行的环境由最后一次运行输出决定。例如,我运行“cd /”任务后,我想运行“ls -l”来显示目录“/”下的所有内容,这是运行“cd /”的结果。
-
Apple 的文档说:“一个 NSTask 对象只能运行一次。”如果有其他方法可以让 shell 子进程在运行时保留并接收 args?
-
如果您想要一个可以向其提供一系列命令的 shell,请不要传递“-c”和命令。而是在没有参数或使用“-s”的情况下启动它。然后将“ls -l\n”(注意换行符)之类的命令写入连接到标准输入的管道。
-
你能给出一些示例代码吗?
标签: objective-c shell nstask nspipe