I'm developing a macOS App which should use Process
to run a shell script. The function somehow like this:
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")
}
}
}
}
and I create a simple ShellTest.sh
file in the Application bundle, then call the shellDemo()
function like this:
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)
}
So far so good, until I use read
to wait for an input value in my script. For example, the script ShellTest.sh
could be :
echo -n "input your name pls:"
read name
echo "hello $name "
The questoin is I can only input the value in my debug console, how could I pass the input value in my code?