Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NSTask-Objective-C执行shell脚本文件_Objective C_Bash_Shell_Nstask - Fatal编程技术网

使用NSTask-Objective-C执行shell脚本文件

使用NSTask-Objective-C执行shell脚本文件,objective-c,bash,shell,nstask,Objective C,Bash,Shell,Nstask,我有一个文件“Script.sh”,其中包含不同的shell命令,如一些默认的写入或创建文件夹、移动文件等。例如: #!/bin/sh defaults write com.apple.finder AppleShowAllFiles -bool TRUE killall Finder 然而,我对Objective C还很陌生,没有找到任何解决方案来使用NSTask运行整个文件。我只找到了一些直接编码到目标C的解决方案 甚至可能吗?让它工作起来了。 只需将Shell脚本的路径设置为NSTask

我有一个文件“Script.sh”,其中包含不同的shell命令,如一些默认的写入或创建文件夹、移动文件等。例如:

#!/bin/sh
defaults write com.apple.finder AppleShowAllFiles -bool TRUE
killall Finder
然而,我对Objective C还很陌生,没有找到任何解决方案来使用NSTask运行整个文件。我只找到了一些直接编码到目标C的解决方案

甚至可能吗?

让它工作起来了。 只需将Shell脚本的路径设置为NSTask的参数并启动它

NSURL *myscript =[[NSBundle mainBundle] URLForResource:@"script" withExtension:@"sh"]; 


NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/bin/sh";

task.arguments =@[myscript.path];
[task launch];
[task waitUntilExit];

这里描述了正确的用法