问题描述
我正在开发一个 macOS 应用程序,它应该使用 Process
来运行 shell 脚本。函数有点像这样:
func shellDemo(launchPath: String?,args: [String]?,onComplete: RunShellScriptResult? = nil,onFail: RunShellScriptResult? = nil) -> Void {
let task = Process()
task.launchPath = launchPath
task.arguments = args
task.launch()
task.terminationHandler = { myTask in
if !myTask.isRunning {
let status = myTask.terminationStatus
if status == 0 {
onComplete?("success")
} else {
onFail?("error")
}
}
}
}
然后我在应用程序包中创建一个简单的 ShellTest.sh
文件,然后像这样调用 shellDemo()
函数:
let path = "/bin/bash"
guard let filePath = Bundle.main.url(forResource: "ShellTest",withExtension: "sh")?.path else {
return
}
let args = ["\(filePath)"]
shellDemo(launchPath: path,args: args) { (success) in
print(success)
} onFail: { (fail) in
print(fail)
}
到目前为止一切顺利,直到我使用 read
等待脚本中的输入值。例如,脚本 ShellTest.sh
可以是:
echo -n "input your name pls:"
read name
echo "hello $name "
问题是我只能在调试控制台中输入值,如何在代码中传递输入值?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)