我想在我的程序中运行一个终端命令。该命令如下所示:
cd /path/to/file/; ./foo HTTPProxy 127.0.0.1它适用于system(),但当我使用NSTask时,它就不起作用了。
system("cd /path/to/file/; ./foo HTTPProxy 127.0.0.1");工作正常,但是
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/path/to/file/./foo"];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task setArguments:[NSArray arrayWithObjects:@"HTTPProxy 127.0.0.1", nil]];
[task launch];
NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(string);不会。输出:
Command-line option 'HTTPProxy 127.0.0.1' with no value. Failing.有谁有主意吗?
发布于 2011-08-01 19:22:13
现在我想我明白了:
[task setArguments:[NSArray arrayWithObjects:@"HTTPProxy", @"127.0.0.1", nil]];这些是调用中与命令行不同的参数...
老生常谈:
您可以尝试设置要执行的当前目录:
– setCurrentDirectoryPath:这基本上就是代码的system版本中的cd效果。
https://stackoverflow.com/questions/6897606
复制相似问题