Ada*_*hti 6 macos bash shell swift
我正在尝试在我的OSX应用程序中使用Swift运行shell命令.
运行echo之类的基本命令可以正常运行,但是后续抛出
"env:node:没有这样的文件或目录"
@IBAction func streamTorrent(sender: AnyObject) {
shell("node", "-v")
}
func shell(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
Run Code Online (Sandbox Code Playgroud)
运行系统命令时,我也会收到"sh:node:command not found".
system("node -v")
Run Code Online (Sandbox Code Playgroud)
更新:
不像下面的一些建议那么好,但我设法将命令回显到一个文件中并在终端中打开并执行:
system("echo node -v > ~/installation.command; chmod +x ~/installation.command; open ~/installation.command")
Run Code Online (Sandbox Code Playgroud)
func shell(command: String) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = ["bash", "-c", command]
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
Run Code Online (Sandbox Code Playgroud)
所以你可以做shell("node -v")
我觉得更方便的事情:
小智 3
对此进行了更多研究。
\n\n安装nodejs
程序使用/usr/local/bin
,它不包含在PATH
从 Finder 启动的应用程序中:
/usr/bin:/bin:/usr/sbin:/sbin\n
Run Code Online (Sandbox Code Playgroud)\n\n该目录包含在via & path_helperPATH
的集合中:bash
/etc/profile
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\n
Run Code Online (Sandbox Code Playgroud)\n\n选项:
\n\n只需写/usr/local/bin/node
而不是node
.
通过属性调整PATH
使用的。NSTask
environment
使用setenv更改PATH
for system
\xe2\x80\x94 也会影响NSTask
.
bash
作为登录 shell执行。有关详细信息,请参阅 手册页 。 归档时间: |
|
查看次数: |
4518 次 |
最近记录: |