2010-09-27 29 views
0

这里的SH文件是我的代码:开始与NSTask

-(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

我米困惑....

有人可以帮我用我的代码?

感谢, 利亚

回答

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

您只需调用NSHomeDirectory得到主目录路径。

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

该文件不存在。/bin目录中没有名为“sh”的目录;因此,在其中没有“应用程序”子目录,在其中没有“jarvis”子目录,在其中没有“脑”子目录,并且没有“server.sh”文件。

请记住,NSTask不是shell。壳牌技巧不适用于此;它不解析参数,插入环境变量或〜(注意你必须明确地这样做),或者shell所做的其他任何事情。您只能使用与其他程序相同的shell。

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