【发布时间】:2021-07-12 23:09:31
【问题描述】:
所以我有了这个按钮,可以让我在终端中运行 /usr/bin/say,效果很好。但是当我尝试更改 executableURL 变量时,我运行 sudo nano /private/etc/hosts 代替,我收到此错误 所以我的问题是,如何通过我的 macOS 应用程序访问终端并运行 sudo nano /private/etc/hosts?
Button(action: {
let executableURL = URL(fileURLWithPath: "/usr/bin/say")
// let executableURL = URL(fileURLWithPath: "sudo nano /private/etc/hosts")
self.isRunning = true
do {
try Process.run(executableURL,
arguments: ["Hello World"],
terminationHandler: { _ in self.isRunning = false })
} catch{
print(error)
}
}) {
Text("Say")
}
编辑:
通过使用这个参数arguments: ["-c", "echo myPassword | sudo -S -- sh -c \"echo \"127.0.0.1www.apple.com \" >> /etc/hosts\""]
行和这条路径 URL(fileURLWithPath: "/bin/zsh") 我能够附加到主机文件。
我遇到的一个问题是我能够附加您在图片中看到的内容(不是第 1 行),但由于某种原因,当我尝试像以前一样做同样的事情时,什么都没有附加。这是一个大问题。我想我必须在附加一些东西后创建一个新行。关于如何做到这一点的任何想法?
【问题讨论】:
标签: ios macos terminal localhost