【问题标题】:NSTask - execute echo commandNSTask - 执行回显命令
【发布时间】:2012-05-30 16:12:54
【问题描述】:

我正在尝试运行一个简单的任务,该任务必须执行回显“Hello World”

这是我的代码:

NSTask *task;
    task = [[NSTask alloc] init];


    [task setLaunchPath:@"/bin/bash"]; 




    NSArray *arguments;

    arguments = [NSArray arrayWithObjects:@"echo","hello world" nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    [task setStandardError: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];



    [task launch];
    //...
    //Code to get task response

一直给我没有这样的文件或目录错误..我做错了什么?

【问题讨论】:

  • 你为什么要让 shell 这样做? NSTask 的优点之一是不必通过 shell。你可以直接运行 echo 命令(单机版,不是 bash 的)。
  • @Peter Hosey Care 发布如何通过 NSTask 独立运行 echo 命令?

标签: objective-c cocoa command nstask


【解决方案1】:

执行命令的正确方式是

bash -c "echo 'hello world'"

这意味着你应该传递的参数是

arguments = [NSArray arrayWithObjects:@"-c", @"echo 'hello world'", nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多