简体   繁体   English

在Swift中运行shell命令

[英]Running shell commands in Swift

I'm trying to run shell commands using Swift in my OSX app. 我正在尝试在我的OSX应用程序中使用Swift运行shell命令。

Running basic commands such as echo work fine but the following throws 运行echo之类的基本命令可以正常运行,但是后续抛出

"env: node: No such file or directory" “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
}

I also get "sh: node: command not found" when running the system command. 运行系统命令时,我也会收到“sh:node:command not found”。

system("node -v")

Update: 更新:

Not as nice as some of the suggestions below, but I managed to echo the command into a file and have it opened and executed in terminal: 不像下面的一些建议那么好,但我设法将命令回显到一个文件中并在终端中打开并执行:

system("echo node -v > ~/installation.command; chmod +x ~/installation.command; open ~/installation.command")
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
}

So you can do shell("node -v") which I find more convenient: 所以你可以做shell("node -v") ,我觉得更方便:

Looked into this a bit more. 再看看这个。

The nodejs installer uses /usr/local/bin , which is not included in the PATH for applications launched from Finder: nodejs安装程序使用/usr/local/bin ,它不包含在从Finder启动的应用程序的PATH中:

/usr/bin:/bin:/usr/sbin:/sbin

The directory is included in the PATH set for bash via /etc/profile & path_helper : 该目录通过/etc/profilepath_helper包含在bashPATH集中:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Options: 选项:

  • Just write /usr/local/bin/node instead of node . 只需编写/usr/local/bin/node而不是node

  • Tweak the PATH used by NSTask via the environment property. 通过environment属性调整NSTask使用的PATH

  • Use setenv to change the PATH for system — will also affect NSTask . 使用setenv更改systemPATH - 也会影响NSTask

  • Execute bash as a login shell. 执行bash作为登录shell。 See the man page for details. 有关详细信息,请参见手册页

"sh: node: command not found" “sh:node:command not found”

Obviously, node is not an absolute path. 显然, node不是绝对路径。

Sounds like you've made changes to ~/.bash_profile or another file to include the location of node in the PATH environment variable . 听起来你已经对~/.bash_profile或其他文件进行了更改,以在PATH 环境变量中包含node的位置。

system is launching bash using sh -c . system正在使用sh -c启动bash

I forget what the rules are exactly, but bash will ignore at least some configuration files when executed this way. 我忘了规则是什么,但是bash在执行这种方式时会忽略至少一些配置文件。 Presumably, your changes to PATH are not being read or applied. 据推测,您对PATH的更改未被阅读或应用。

"env: node: No such file or directory" “env:node:没有这样的文件或目录”

Again, node is not an absolute path. 同样, node不是绝对路径。 NSTask doesn't use a shell, so any processes you execute will inherit your application's environment variables. NSTask不使用shell,因此您执行的任何进程都将继承应用程序的环境变量。 In most cases, this means the default PATH . 在大多数情况下,这意味着默认PATH

If you need a custom environment for NSTask , grab the environment dictionary from NSProcessInfo , make your changes & then set NSTask 's environment property. 如果您需要NSTask的自定义环境, NSTaskNSProcessInfo获取environment字典,进行更改然后设置NSTaskenvironment属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM