NSTask启动路径不可访问 [英] NSTask launch path not accessible
问题描述
我在我的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。 app / Contents / Resources / script.sh
2010-07-10 17:53:26.386 tester [5023 :a0f] launch path not accessible
2010-07-10 17:53:26.386 tester[5023:a0f] launch path not accessible
推荐答案
你需要做的是获取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屋!