bash 检测是否安装了自制软件包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20802320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:34:31  来源:igfitidea点击:

Detect if homebrew package is installed

bashshellhomebrew

提问by iltempo

I'm about to write a shell script to detect if several homebrew packages are installed in the system. Is there a way to use a brew command to achieve that?

我即将编写一个 shell 脚本来检测系统中是否安装了多个自制程序包。有没有办法使用 brew 命令来实现这一点?

I tried using the exit code of brew install <formula> --dry-run. But this builds the package if it is missing.

我尝试使用brew install <formula> --dry-run. 但是如果它丢失,这会构建包。

回答by Holger Just

You can use

您可以使用

brew ls --versions myformula

to output the installed versions of the respective formula. If the formula is not installed, the output will be empty.

输出相应公式的已安装版本。如果未安装公式,则输出将为空。

When using a recent versions of homebrew, which you can get with brew update, you can just run this (thanks Slaven):

当使用最新版本的自制软件时,你可以使用它brew update,你可以运行这个(感谢 Slaven):

if brew ls --versions myformula > /dev/null; then
  # The package is installed
else
  # The package is not installed
fi

That said, it is probably a good idea to check for the existence of the tool at all and not just checking for the respective homebrew package (e.g. by searching for the executable in the $PATH). People tend to install tools in a rather large amount of ways in practice, with homebrew being just one of them.

也就是说,检查该工具是否存在可能是一个好主意,而不仅仅是检查相应的自制程序包(例如,通过在 中搜索可执行文件$PATH)。人们在实践中倾向于以相当多的方式安装工具,自制软件只是其中之一。

回答by Johannes Weiss

What about?

关于什么?

for pkg in macvim ngrep other needed packages; do
    if brew list -1 | grep -q "^${pkg}$"; then
        echo "Package '$pkg' is installed"
    else
        echo "Package '$pkg' is not installed"
    fi
done

回答by timotheecour

# install if we haven't installed any version
brew ls --versions $lib || brew install $lib
# install if we haven't installed latest version
brew outdated $lib || brew install $lib