简体   繁体   English

从Objective-C运行shell脚本

[英]Run shell script from Objective-C

I'm trying to run a shell script that is in an NSTextView . 我正在尝试运行NSTextView的shell脚本。 I know I can use NSTask to do this, but I was wondering what the best way would be, if the script only exists in memory (the text view) but not on disk. 我知道我可以使用NSTask来做到这一点,但我想知道最好的方法是什么,如果脚本只存在于内存(文本视图)中但不存在于磁盘上。

Should I temporarily write it to disk or somehow run it directly as string? 我应该暂时将其写入磁盘或以某种方式直接将其作为字符串运行吗?

And what parameters do I pass to NSTask ? 我将哪些参数传递给NSTask Do I have to extract the shebang line and pass it as launchPath ? 我是否必须提取shebang行并将其作为launchPath传递?

You can execute /bin/bash as your NSTask , and setStandardInput to an NSPipe . 您可以执行/bin/bash作为NSTask ,并将setStandardInput作为NSPipe Then write the NSString containing your script to the pipe via its fileHandleForWriting to avoid creating a file on disk. 然后通过其fileHandleForWriting将包含脚本的NSString写入管道,以避免在磁盘上创建文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM