NSProcessInfo返回不同于“echo $ PATH”的PATH。 [英] NSProcessInfo returns different PATH than "echo $PATH"

查看:175
本文介绍了NSProcessInfo返回不同于“echo $ PATH”的PATH。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编程找出是否有一个特定的二进制文件在系统PATH。为了获得环境,我使用了

I am trying to programatically figure out whether there is a specific binary in the system PATH. To get the environment I used both

NSString* path = [[[NSProcessInfo processInfo] environment] objectForKey:@"PATH"];

NSString* path2 = [NSString stringWithUTF8String: getenv("PATH")];

两个都产生相同的结果,两种情况都不同于 echo $ PATH 路径 path2 不包含通过 /etc/paths.d 设置的路径,因此问题是如何获取环境PATH从控制台编程返回?

both yielding the same result, in both cases different then echo $PATH in console. Both path and path2 does not contain paths set via /etc/paths.d, so the question is how to get the the environment PATH as returned from console programatically?

推荐答案

NSProcessInfo只是访问当前进程的信息。例如下面我在可可中执行相同的echo $ PATH命令,并得到NSProcessInfo显示的相同输出。所以在终端中执行相同的命令时。你会得到不同的输出。因为它显示了终端当前进程的路径。如果你想看到两个相同的输出,你可以在终端 launchctl getenv PATH 中执行这个命令,这相当于[[[NSProcessInfo processInfo] environment] objectForKey:@PATH]; p>

NSProcessInfo will just access information about current process. For example below i am executing the same echo $PATH command in cocoa and am getting the same output which NSProcessInfo is displaying. So in the terminal when you execute the same command. You will get different ouput. Because it is showing the path of current process in terminal. If you want to see the same output of both you can execute this command in terminal launchctl getenv PATH which will be equivalent to [[[NSProcessInfo processInfo] environment] objectForKey:@"PATH"];

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
[task setArguments:[NSArray arrayWithObjects: @"-c", @"echo $PATH",nil]];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@",response);

这篇关于NSProcessInfo返回不同于“echo $ PATH”的PATH。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆