NSTask 启动路径不可访问

发布于 2024-09-08 15:09:09 字数 1115 浏览 12 评论 0原文

我在我的 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];

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

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] 启动路径无法访问

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万人眼中万个我 2024-09-15 15:09:10

[任务启动]之前添加此行:

[task setLaunchPath:@"/bin/sh"];

add this line before [task launch]:

[task setLaunchPath:@"/bin/sh"];
梦境 2024-09-15 15:09:10

您需要做的是获取 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.

金橙橙 2024-09-15 15:09:10

当脚本本身未标记为可执行时,会发生此错误。打开终端窗口并转到脚本在项目目录中的位置,然后输入:

chmod +x script.sh

Clean and build,您可以直接使用脚本 - 这也意味着您可以向其传递参数。

This error occurs when the script itself is not marked as executable. Open a terminal window and go to where the script is in your project directory, then enter this:

chmod +x script.sh

Clean and build and you can use the script directly - this also means you can pass arguments to it.

浅唱々樱花落 2024-09-15 15:09:10

当我将脚本的名称(存储在应用程序包中)从 foo.sh 更改为 foo.command 时,我避免了此错误。

let path = Bundle.main.path(forResource: "foo", ofType: "command")

.command 扩展名用于 Ray NSTask / Process 的 Wenderlich 教程。我似乎在任何地方都找不到这个记录(我还确保该脚本可以通过 chmod +x foo.sh 执行)。

When I change the name of my script (stored in the App Bundle) from foo.sh to foo.command I avoid this error.

let path = Bundle.main.path(forResource: "foo", ofType: "command")

The .command extension is what is used in the Ray Wenderlich tutorial for NSTask / Process. I can't seem to find this documented anywhere (I also make sure the script is executable via chmod +x foo.sh).

中二柚 2024-09-15 15:09:10

因为[任务等待退出];

because of [task waitUntilExit];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文