0

我想创建一个 SwiftUI MacOs 应用程序来从我的应用程序运行 shell 脚本。因此,我想打开终端并粘贴 .sh 或此脚本的内容并运行它。首先,我收到了一个权限被拒绝错误,我通过删除沙盒模式解决了这个错误。

但是现在脚本中的所有命令,如 npm 或 mvn 都会导致我出现错误: "zsh:1: command not found: npm\n"

你有解决我的问题的方法吗?

struct ContentView: View {
    
    var title : String
    
    var body: some View {
        VStack {
          
            Button(action: {
                runCommand()
            }) {
                Text("Run")
            }
        }
    }
}


func runCommand() {

    let path = "/Users/User1/Desktop/test.sh"
    print( shell("/Users/User1/Desktop/test.sh start") )
}

func shell(_ command: String) -> (String?) {
    let task = Process()

    task.launchPath = "/bin/zsh"
    task.arguments = ["-c", command]

    let pipe = Pipe()
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8)
    task.waitUntilExit()
    return (output)
}
4

0 回答 0