如何在Swift中执行shell脚本?
我正在使用Swift创建一个macOS应用程序,需要下载并运行shell脚本。我已经能够下载带有如何在Swift中执行shell脚本?,swift,bash,appstore-sandbox,Swift,Bash,Appstore Sandbox,我正在使用Swift创建一个macOS应用程序,需要下载并运行shell脚本。我已经能够下载带有curl的脚本,但无法运行它。我正在使用函数from来运行其他命令。当我在Xcode中使用应用程序沙盒时,我得到错误/bin/bash:./file.sh:权限被拒绝。当我尝试更改文件权限时,出现错误chmod:Unable to change file.sh:Operation not allowed 这是我的密码: func shell(_ command: String) -> Strin
curl
的脚本,但无法运行它。我正在使用函数from来运行其他命令。当我在Xcode中使用应用程序沙盒
时,我得到错误/bin/bash:./file.sh:权限被拒绝
。当我尝试更改文件权限时,出现错误chmod:Unable to change file.sh:Operation not allowed
这是我的密码:
func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/bin/bash"
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
return output
}
shell("curl https://www.example.com/file.sh -o file.sh")
shell("./file.sh")
输出:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
1 16.0M 1 224k 0 0 231k 0 0:01:11 --:--:-- 0:01:11 231k
47 16.0M 47 7808k 0 0 4064k 0 0:00:04 0:00:01 0:00:03 4062k
100 16.0M 100 16.0M 0 0 6037k 0 0:00:02 0:00:02 --:--:-- 6037k
/bin/bash: ./file.sh: Permission denied
您必须设置执行权限才能运行脚本:
chmod+x file.sh
我试过了,它仍然说权限被拒绝
如果应用程序是沙盒,则无法使用进程运行shell脚本。仅当用户将脚本放入应用程序脚本并使用nsUserUnixtaskp运行脚本时,请读取