使用 NSTask 启动 SH 文件

发布于 2024-09-24 19:45:41 字数 963 浏览 1 评论 0原文

这是我的代码:

-(void)startTask{
NSString * cmd = @"/bin/sh";
pty_ = [[PseudoTTY alloc] init];

NSTask * task = [[NSTask alloc] init];
[task setStandardInput:[pty_ slaveFileHandle]];
[task setStandardOutput:[pty_ slaveFileHandle]];
[task setStandardError:[pty_ slaveFileHandle]];

[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];
[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(didRead:)
                   name:NSFileHandleReadCompletionNotification
                 object:[pty_ masterFileHandle]];

[[pty_ masterFileHandle] readInBackgroundAndNotify];

[task launch];

[self insertText:
    [NSString stringWithFormat:@"Started %@ on terminal %@", cmd, [pty_ name]]];

}

但是,我需要它来启动 SH 文件,而不是这个: /applications/brain/server.sh

我很困惑......

有人可以帮助我处理我的代码吗?

谢谢, 以利亚

Here is my code:

-(void)startTask{
NSString * cmd = @"/bin/sh";
pty_ = [[PseudoTTY alloc] init];

NSTask * task = [[NSTask alloc] init];
[task setStandardInput:[pty_ slaveFileHandle]];
[task setStandardOutput:[pty_ slaveFileHandle]];
[task setStandardError:[pty_ slaveFileHandle]];

[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];
[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(didRead:)
                   name:NSFileHandleReadCompletionNotification
                 object:[pty_ masterFileHandle]];

[[pty_ masterFileHandle] readInBackgroundAndNotify];

[task launch];

[self insertText:
    [NSString stringWithFormat:@"Started %@ on terminal %@", cmd, [pty_ name]]];

}

But, instead of this, I need it to start an SH file: /applications/brain/server.sh

I'm confused....

Can someone help me with my code?

thanks,
Elijah

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

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

发布评论

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

评论(2

北方的巷 2024-10-01 19:45:41
[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];

您只需调用 NSHomeDirectory 即可获取主目录路径。

[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

该文件不存在。 /bin目录下没有名为“sh”的目录;因此,其中没有“applications”子目录,其中没有“jarvis”子目录,其中没有“brain”子目录,并且其中没有“server.sh”文件。

请记住,NSTask 不是 shell。 Shell 技巧对此不起作用;它不会解析参数、插入环境变量或 ~(请注意,您必须显式地执行此操作)或 shell 执行的任何其他操作。您只能像使用任何其他程序一样使用 shell。

您需要将启动路径设置为 shell 的路径,并将 shell 脚本的路径作为第一个参数传递。或者,如果您知道 shell 脚本文件是可执行的(并且具有正确的 shebang),您可以将脚本文件的路径作为启动路径传递并省略参数。

[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];

You can just call NSHomeDirectory to get the home directory path.

[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

This file does not exist. There is no directory named “sh ” within the /bin directory; as such, there is no “applications” subdirectory within that, no “jarvis” subdirectory within that, no “brain” subdirectory within that, and no “server.sh” file within that.

Remember that NSTask is not the shell. Shell tricks don't work on it; it doesn't parse arguments, interpolate environment variables or ~ (notice that you had to do that explicitly), or anything else the shell does. You can only use the shell the same as any other program.

You need to set the launch path to the path to the shell, and pass the path to the shell script as the first argument. Alternatively, if you know that the shell script file is executable (and has the correct shebang in it), you can pass the path to the script file as the launch path and omit the arguments.

天涯沦落人 2024-10-01 19:45:41

尝试:(

// make sure server.sh begins with #!/bin/sh
NSString * cmd = @"/applications/brain/server.sh";  
// ...
[task setLaunchPath:cmd];

原始代码来自: http://amath.colorado.edu/pub/ mac/programs/PseudoTTY.zip)

Try:

// make sure server.sh begins with #!/bin/sh
NSString * cmd = @"/applications/brain/server.sh";  
// ...
[task setLaunchPath:cmd];

(original code from: http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip)

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