2014-08-28 40 views
8

我試圖運行的shell腳本NSTask用下面的代碼:與NSTask運行shell腳本引起的posix_spawn錯誤

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/Users/username/connect.sh"]; 
[task launch]; 

,但我得到An uncaught exception was raisedCouldn't posix_spawn: error 8

如果我只是運行該腳本在終端,一切正常。

這裏是腳本包含:

if [ ! -d ~/Remote/username/projects ] 
then 
     sshfs -C -p 22 [email protected]:/home/username ~/Remote/username   
fi 

回答

2

您需要使用setLaunchPath這樣的:

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

然後使用setArguments您的腳本:

[task setArguments: [NSArray arrayWithObjects: @"~/connect.sh", nil]]; 
2

你也可以將#!/bin/bash添加到腳本的開頭:

#!/bin/bash 

if [ ! -d ~/Remote/username/projects ] 
then 
    sshfs -C -p 22 [email protected]:/home/username  ~/Remote/username   
fi