无法访问NSTask启动路径

has*_*anm 9 cocoa path objective-c nstask nsbundle

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

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];
Run Code Online (Sandbox Code Playgroud)

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

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

sti*_*fin 5

之前添加此行[task launch]:

[task setLaunchPath:@"/bin/sh"];
Run Code Online (Sandbox Code Playgroud)


jer*_*jer 4

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

  • 如果脚本本身带有参数,你会怎么做? (2认同)