【问题标题】:"zsh: command not found:" when running Ruby commands“zsh: command not found:” 运行 Ruby 命令时
【发布时间】:2021-02-23 08:50:59
【问题描述】:

我仍然可以使用 ls 或 pwd 等核心命令,但某些与 Ruby 相关的命令无法识别。不确定这是否适用于其他语言,但我似乎对 npm 或类似的东西没有任何问题。

如果有帮助,我也在使用 macOS 卡塔利娜 版本 10.15.7

run `bundle update --bundler`
zsh: command not found: run

我不知道如何重置 zsh 或如何解决此问题。这是 $PATH 变量的内容:

/bin:/usr/bin:/usr/local/bin:/Users/peter/bin:/usr/local/bin:/Users/peter/bin:/usr/local/bin:/Users/peter/.rvm/gems/ruby-2.7.0/bin:/Users/peter/.rvm/gems/ruby-2.7.0@global/bin:/Users/peter/.rvm/rubies/ruby-2.7.0/bin:/Users/peter/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/peter/.rvm/bin:/Users/peter/.rvm/bin:/Users/peter/.rvm/bin

当我在终端 'vim ~/.zshrc' 中运行时,它会返回以下内容

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/Users/peter/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# alias brewup=‘brew update; brew upgrade; brew cleanup; brew doctor’

# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"
export PATH

知道问题是什么或如何解决吗?如果您需要我提供更多信息,请告诉我。我以前从未遇到过这个问题,所以不知道如何解决它。

【问题讨论】:

  • 在 VSCode 和我的终端中尝试了相同的消息“未定义的局部变量或方法 `bundler' for main:Object (NameError)”
  • 你应该在 zsh 中直接使用bundle update --bundler,而不使用run。我猜你正在将 ruby​​ 代码与 zsh 混合,但我不知道足够的 ruby​​ 来确定。
  • 我之前尝试过,这确实成功了!谢谢! :) 我想知道的一件事是所有未来的安装是否都可以,或者我的 $PATH 变量是否正确。到目前为止,一切似乎都很好。
  • @PeterStevens :您可以自己轻松修复 PATH 问题:只需打印 PATH 或 path 变量并查看它是否包含您要执行的程序所在的目录(在您的示例中:run)。当然这意味着你必须知道你的程序安装在哪里。

标签: ruby shell path zsh macos-catalina


【解决方案1】:

修复SHELL和PATH环境变量

您的 PATH 未使用正确的 SHELL 变量导出,或者您遇到了其他一些与 PATH 相关的问题。您可以通过多种方式解决此问题,但我会建议其中两种。

反引号在您定义的 shell(例如 ENV['SHELL'])中运行命令,因此如果 zsh 不是您当前的 SHELL,或者如果您的 shell 不在您当前的 PATH 中,那么您重新会有问题。在运行您的应用程序之前更新您的 PATH,或者将其添加到您的 Ruby 脚本中,如下所示:

ENV['SHELL'] = '/bin/zsh'
ENV['PATH']  = [ENV['SHELL'], ENV['PATH']].join ?:

使用 Kernel#system 绕过 Shell

或者,如果您不需要捕获标准输出,您可以使用Kernel#system 的无外壳版本。例如:

# this may not work if you're not using a system ruby
system('/path/to/bundle', 'update', '--bundler')

更多全局修复

无论您采取什么方法,问题很可能是相同的:您没有导出适当的 SHELL 或 PATH 变量,因此您可能需要查看在:

  1. 您的默认用户 shell,例如dscl . -read ~/ UserShell。
  2. 您的 shell 的各种启动文件。
  3. 您的终端设置,以确保它正在启动登录 shell。

某些东西 设置不正确,无论是在您的 shell 环境还是您的 IDE 中,所以您需要跟踪它。这应该让您有一个良好的开端。

【讨论】:

  • 只是一个更新,我注意到如果我只运行 'bundle update --bundler' 而不在开始时运行位,它似乎工作正常。不过,不确定像 run 这样的基本命令有多重要。
  • “反引号在你定义的 shell 中运行命令”——有趣,我不知道。我一直认为它们是根据操作系统硬连线到cmd.exe 或/bin/sh。
  • @JörgWMittag:你说对了一半。定义的shell可以被环境变量覆盖,但这个变量不是SHELL。它是RUBYSHELL,如果没有设置并且我们在Windows 上,则使用变量COMSPEC。仅当未设置变量时,才会应用标准(/bin/sh)。
猜你喜欢
  • 2022-07-05
  • 2021-12-14
  • 2020-11-08
  • 1970-01-01
  • 2018-01-13
  • 2021-05-16
  • 2021-01-28
  • 2021-06-28
  • 2020-06-14
相关资源
最近更新 更多