使用 NSTask 启动 SH 文件
这是我的代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需调用 NSHomeDirectory 即可获取主目录路径。
该文件不存在。 /bin目录下没有名为“sh”的目录;因此,其中没有“applications”子目录,其中没有“jarvis”子目录,其中没有“brain”子目录,并且其中没有“server.sh”文件。
请记住,NSTask 不是 shell。 Shell 技巧对此不起作用;它不会解析参数、插入环境变量或 ~(请注意,您必须显式地执行此操作)或 shell 执行的任何其他操作。您只能像使用任何其他程序一样使用 shell。
您需要将启动路径设置为 shell 的路径,并将 shell 脚本的路径作为第一个参数传递。或者,如果您知道 shell 脚本文件是可执行的(并且具有正确的 shebang),您可以将脚本文件的路径作为启动路径传递并省略参数。
You can just call
NSHomeDirectory
to get the home directory path.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.
尝试:(
原始代码来自: http://amath.colorado.edu/pub/ mac/programs/PseudoTTY.zip)
Try:
(original code from: http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip)