我不知道为什么这个方法返回一个空字符串:

- (NSString *)installedGitLocation {
    NSString *launchPath = @"/usr/bin/which";

    // Set up the task
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:launchPath];
    NSArray *args = [NSArray arrayWithObject:@"git"];
    [task setArguments:args];

    // Set the output pipe.
    NSPipe *outPipe = [[NSPipe alloc] init];
    [task setStandardOutput:outPipe];

    [task launch];

    NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
    NSString *path = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    return path;
}

如果强似@"git"作为参数,我通过@"which"我得到/usr/bin/which返回预期。因此,至少在原则作品。

从终端

$ which which
$ /usr/bin/which
$
$ which git
$ /usr/local/git/bin/git

因此,它在那里工作。

我能想到的唯一的事情就是which不是通过我的环境中的所有路径进行搜索。

这是推动我疯了!没有人有任何想法?

编辑:看起来这是关于设立或者NSTask或用户的shell(如〜/ .bashrc)中,以便正确的环境($ PATH)由NSTask看到

有帮助吗?

解决方案

经由NSTask运行任务使用fork()exec()实际运行的任务。用户的交互式shell是不参与的。由于$PATH是(大体上)壳的概念,它不会当你谈论一些其他的方式运行的进程适用。

其他提示

尝试,

    [task setLaunchPath:@"/bin/bash"];
    NSArray *args = [NSArray arrayWithObjects:@"-l",
                     @"-c",
                     @"which git",
                     nil];
    [task setArguments: args];

此为我工作在Snow Leopard上;我没有任何其他的系统上进行测试。 -l(小写L)告诉Bash“行为,就好像它已经被援引作为登录shell”,并在这个过程中它拿起我的正常的$ PATH。这并没有为我工作,如果启动路径设置为/位/ SH,即使-l。

是/ usr /本地/ GIT中/在你的$ PATH斌,当你运行的程序?我认为which只着眼于用户的$ PATH。

看看这个问题找出的位置在可可可执行文件。它看起来像基本的问题是相同的。不幸的是还没有很好的整洁,但有一些有用的信息在那里。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top