我正在尝试从我的应用程序运行终端命令/脚本,一切正常但是当命令错误且无法执行时我会得到类似这样的内容:
但是这个“/ bin / bash:line ...”字符串不在我从任务中得到的输出字符串中,有没有办法在我的应用程序中获取这些错误或以任何方式得到通知?< / p>
我的代码
// Create a new task
let task: Process = Process()
task.environment = env
task.launchPath = "/usr/bin/env"
task.arguments = ["/bin/bash", "-c", command.scriptCode]
// Assign output pipes
let pipe: Pipe = Pipe()
let outHandle: FileHandle = pipe.fileHandleForReading
task.standardOutput = pipe
outHandle.readabilityHandler = { pipe in
if let line = String(data: pipe.availableData, encoding: String.Encoding.utf8) {
if line.contains("command not found") {
// never triggered
} else {
print("New ouput: \(NSDate() )\(line)")
}
} else {
print("Error decoding data: \(pipe.availableData)")
}
}