[英]Running a shell script from Swift/MacOS
過去我可以使用這個函數成功運行 shell 腳本:
typealias runShellClosure = (_ jsonString:String) -> Void
func runShell(_ command: String, completion: runShellClosure) {
let task = Process()
task.launchPath = "/bin/zsh"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
completion(output)
}
今天我安裝了 YQ(使用 Homebrew) - YQ 將 YAML 文件轉換為 JSON。
在終端中進行測試:
yq r -j ~/Desktop/xxxxxxx/swagger/yml/up-api.yaml
並收到了預期的結果。 在我的應用程序中,我將相同的命令傳遞給 runShell 函數:
我收到command not found
錯誤。
不知道我做錯了什么。 我剛剛切換到 Catalina,但將 shell 切換回 Bash 會返回相同的結果。
謝謝
編輯:我可以運行這個命令:
ls ~/Desktop/Xandr/swagger/yml/
並得到預期的結果。 此外,沙盒也被禁用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.