【问题标题】:Run terminal command with NSTask使用 NSTask 运行终端命令
【发布时间】:2011-10-17 09:03:18
【问题描述】:

我想在我的程序中运行终端命令。 该命令如下所示:

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.

有人有想法吗?

【问题讨论】:

    标签: objective-c nstask


    【解决方案1】:

    现在我想我明白了:

    [task setArguments:[NSArray arrayWithObjects:@"HTTPProxy", @"127.0.0.1", nil]];
    

    这些是您从命令行调用的单独参数...

    旧答案:

    您可以尝试设置当前目录执行:

    – setCurrentDirectoryPath:
    

    这基本上是你代码的system版本中cd的效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2018-12-28
      • 1970-01-01
      相关资源
      最近更新 更多