NSTask 能否调用一个 bash shell,其中有一个读取命令并等待输入值?

问题描述

我正在开发一个 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...