NSTask 启动路径不可访问 [英] NSTask launch path not accessible

查看:27
本文介绍了NSTask 启动路径不可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Cocoa 项目中使用以下代码来调用我制作的脚本.该脚本与项目位于同一文件夹中,甚至显示在 Xcode 的Resources"文件夹下.找到了正确的路径,但它仍然说该路径不可访问.请帮忙.

I am using the following code in my Cocoa project to call a script I made. The script is in the same folder as the project and even shows up under the "Resources" folder in Xcode. The proper path is found, but it still says that the path is not accessible. Help please.

NSBundle *mainBundle=[NSBundle mainBundle];
NSString *path=[mainBundle pathForResource:@"script" ofType:@"sh"];

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: path];

NSLog (path);

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

NSFileHandle *file = [pipe fileHandleForReading];

[task launch];
[task waitUntilExit];

NSData *data = [ddFile readDataToEndOfFile];

NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
HDSpeed = [f output];

[f release];    
[task release];

我在调试器中得到的输出是:

The output I get in the debugger is:

2010-07-10 17:53:26.384 tester[5023:a0f]/Users/guest/Library/Developer/Xcode/DerivedData/tester-bhukztmqjwoqrwereagsshvtbfqx/Build/Products/Debug/Tester./Resources/script.sh

2010-07-10 17:53:26.386 tester[5023:a0f] 启动路径不可访问

推荐答案

您需要做的是获取 shell 二进制文件,并将您的脚本作为参数传递.因此,如果 shell 脚本是针对 bash 编写的,请获取 bash 解释器,并将一个参数列表传递给它,其中包含一个参数:script.sh 的路径.

What you will need to do is get the shell binary, and pass your script as an argument. So if the shell script is written targeting bash, get the bash interpreter, and pass it an argument list with one argument: the path to your script.sh.

这篇关于NSTask 启动路径不可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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