如何分辨homebrew是否安装在Mac OS X上?

时间:2021-10-24 16:44:39

I am doing some Rails programming and I consistently see Homebrew referenced in solutions around the web but have never used it.

我正在做一些Rails编程,我经常看到Homebrew在web上的解决方案中被引用,但从未使用过它。

I also notice Homebrew in the terminal version 2.9 as an option next to "Shell -> New" from the terminal drop down but when I select homebrew and issue commands, they fail.

我还注意到,在终端版本2.9中,Homebrew是终端“Shell -> New”旁边的一个选项。

Usually with the "command not found" error.

通常使用“命令未找到”错误。

Strangely enough I have been unable to locate a simple command to determine whether brew is installed or not.

奇怪的是,我无法找到一个简单的命令来确定是否安装了brew。

How do I check to see if Homebrew is already installed on my Mac?

如何检查Homebrew是否已经安装在我的Mac上?

7 个解决方案

#1


40  

brew help. If brew is there, you get output. If not, you get 'command not found'. If you need to check in a script, you can work out how to redirect output and check $?.

酿造的帮助。如果啤酒在那里,你就会得到产出。如果没有,你会得到“未找到命令”。如果您需要检入脚本,您可以找到如何重定向输出和检查$?

#2


33  

The standard way of figuring out if something is installed is to use which.

确定是否安装了什么东西的标准方法是使用哪个。

If Brew is installed.

如果安装酿造。

>>> which brew
/usr/local/bin/brew

If Brew is not installed.

如果不安装Brew。

>>> which brew
brew not found

Note: The "not installed" message depends on your shell. zsh is shown above. bash will just not print anything. csh will say brew: Command not found. In the "installed" case, all shells will print the path.)

注意:“未安装”消息取决于您的shell。zsh如上所示。bash不会打印任何东西。csh会说brew:命令未找到。在“已安装”情况下,所有shell将打印路径。

It works with all command line programs. Try which grep or which python. Since it tells you the program that you're running, it's helpful when debugging as well.

它适用于所有命令行程序。尝试使用哪个grep或哪个python。因为它告诉您正在运行的程序,所以在调试时也很有用。

#3


27  

I use this to perform update or install:

我用它来执行更新或安装:

which -s brew
if [[ $? != 0 ]] ; then
    # Install Homebrew
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    brew update
fi

#4


4  

I just type brew -v in terminal if you have it it will respond with the version number installed.

我只是在终端上输入brew -v,如果你有的话,它将会响应版本号的安装。

#5


2  

While which is the most common way of checking if a program is installed, it will tell you a program is installed ONLY if it's in the $PATH. So if your program is installed, but the $PATH wasn't updated for whatever reason*, which will tell you the program isn't installed.

虽然这是最常用的检查程序是否安装的方式,但它会告诉您程序只有在$PATH中才安装。因此,如果您的程序已经安装,但是$PATH由于任何原因没有更新*,这将告诉您程序没有安装。

(*One example scenario is changing from Bash to Zshell and ~/.zshrc not having the old $PATH from ~/.bash_profile)

(*一个示例场景是从Bash更改为Zshell和~/。zshrc没有来自~/.bash_profile的旧$路径)

command -v foo is a better alternative to which foo. command -v brew will output nothing if Homebrew is not installed

命令-v foo是更好的选择。如果没有安装Homebrew,命令-v brew不会输出任何内容

command -v brew

Here's a sample script to check if Bash is installed, install it if it isn't, update if it is.

这里有一个示例脚本,用于检查Bash是否已安装,如果未安装,请安装,如果已安装,请更新。

if [[ $(command -v brew) == "" ]]; then
    echo "Installing Hombrew"
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    echo "Updating Homebrew"
    brew update
fi

#6


0  

[ ! -f "`which brew`" ] && echo "not installed"

Explaination: If brew is not installed run command after &&

说明:如果没有安装brew,请在&&之后运行命令

#7


0  

Another one possible way:

另一个可能的方法:

# Check if Ninja is installed
if ! which ninja > /dev/null
then
echo 'Ninja installation...'
brew install ninja
fi

#1


40  

brew help. If brew is there, you get output. If not, you get 'command not found'. If you need to check in a script, you can work out how to redirect output and check $?.

酿造的帮助。如果啤酒在那里,你就会得到产出。如果没有,你会得到“未找到命令”。如果您需要检入脚本,您可以找到如何重定向输出和检查$?

#2


33  

The standard way of figuring out if something is installed is to use which.

确定是否安装了什么东西的标准方法是使用哪个。

If Brew is installed.

如果安装酿造。

>>> which brew
/usr/local/bin/brew

If Brew is not installed.

如果不安装Brew。

>>> which brew
brew not found

Note: The "not installed" message depends on your shell. zsh is shown above. bash will just not print anything. csh will say brew: Command not found. In the "installed" case, all shells will print the path.)

注意:“未安装”消息取决于您的shell。zsh如上所示。bash不会打印任何东西。csh会说brew:命令未找到。在“已安装”情况下,所有shell将打印路径。

It works with all command line programs. Try which grep or which python. Since it tells you the program that you're running, it's helpful when debugging as well.

它适用于所有命令行程序。尝试使用哪个grep或哪个python。因为它告诉您正在运行的程序,所以在调试时也很有用。

#3


27  

I use this to perform update or install:

我用它来执行更新或安装:

which -s brew
if [[ $? != 0 ]] ; then
    # Install Homebrew
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    brew update
fi

#4


4  

I just type brew -v in terminal if you have it it will respond with the version number installed.

我只是在终端上输入brew -v,如果你有的话,它将会响应版本号的安装。

#5


2  

While which is the most common way of checking if a program is installed, it will tell you a program is installed ONLY if it's in the $PATH. So if your program is installed, but the $PATH wasn't updated for whatever reason*, which will tell you the program isn't installed.

虽然这是最常用的检查程序是否安装的方式,但它会告诉您程序只有在$PATH中才安装。因此,如果您的程序已经安装,但是$PATH由于任何原因没有更新*,这将告诉您程序没有安装。

(*One example scenario is changing from Bash to Zshell and ~/.zshrc not having the old $PATH from ~/.bash_profile)

(*一个示例场景是从Bash更改为Zshell和~/。zshrc没有来自~/.bash_profile的旧$路径)

command -v foo is a better alternative to which foo. command -v brew will output nothing if Homebrew is not installed

命令-v foo是更好的选择。如果没有安装Homebrew,命令-v brew不会输出任何内容

command -v brew

Here's a sample script to check if Bash is installed, install it if it isn't, update if it is.

这里有一个示例脚本,用于检查Bash是否已安装,如果未安装,请安装,如果已安装,请更新。

if [[ $(command -v brew) == "" ]]; then
    echo "Installing Hombrew"
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    echo "Updating Homebrew"
    brew update
fi

#6


0  

[ ! -f "`which brew`" ] && echo "not installed"

Explaination: If brew is not installed run command after &&

说明:如果没有安装brew,请在&&之后运行命令

#7


0  

Another one possible way:

另一个可能的方法:

# Check if Ninja is installed
if ! which ninja > /dev/null
then
echo 'Ninja installation...'
brew install ninja
fi