bash 在 OS X 上找到 brew 的安装位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20728589/
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-18 09:03:58  来源:igfitidea点击:

Find the install location of brew on OS X

bashhomebrew

提问by dwkns

I'm creating a BASH scrip which requires a couple of applications to be installed. ffmpegand sox

我正在创建一个 BASH 脚本,它需要安装几个应用程序。ffmpegsox

To ensure they are in place when my script runs I first check for the installation of Homebrew with :

为了确保它们在我的脚本运行时就位,我首先使用以下命令检查 Homebrew 的安装:

#!/bin/bash
which -s brew
if [[ $? != 0 ]] ; then
    # Install Homebrew
    /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
fi

Then I check that soxand ffmpegare installed with :

然后我检查soxffmpeg安装:

echo "---- checking for sox ----"
which -s sox || /usr/local/bin/brew install sox

echo "---- checking for ffmpeg ----"
which -s ffmpeg || /usr/local/bin/brew install ffmpeg

The problem I am facing is when Homebrew is installed but in a non-standard location.

我面临的问题是 Homebrew 已安装但位于非标准位置。

I have to use the full path to Homebrew because this script is being run within Playtypus.

我必须使用 Homebrew 的完整路径,因为此脚本正在Playtypus中运行。

So the question is : How can I reliably get the installed path of Homebrew in a BASH script?

所以问题是:如何在 BASH 脚本中可靠地获取 Homebrew 的安装路径?

回答by dwkns

Answering my own question...

回答我自己的问题...

You can test the output of which brewand deal with things accordingly. To gracefully deal with the case where Homebrew is not installed you can use if which brew 2> /dev/nullwhich redirects stderrto /dev/null.

您可以测试输出which brew并相应地处理事情。为了优雅地处理未安装 Homebrew 的情况,您可以使用if which brew 2> /dev/nullwhich 重定向stderr/dev/null.

brew --prefixis also useful here as it give the path to where Homebrew installed applications are symlinked to, rather than their actual install path.

brew --prefix在这里也很有用,因为它给出了 Homebrew 安装的应用程序符号链接到的路径,而不是它们的实际安装路径。

A script which works and shows this working :

一个可以工作并显示此工作的脚本:

#!/bin/bash
if which brew 2> /dev/null; then
    brewLocation=`which brew`
    appLocation=`brew --prefix`
    echo "Homebrew is installed in $brewLocation"
    echo "Homebrew apps are run from $appLocation"
else
   echo "Can't find Homebrew"
   echo "To install it open a Terminal window and type :"
   echo /usr/bin/ruby -e \"$\(curl\ \-fsSL\ https\:\/\/raw\.github\.com\/Homebrew\/homebrew\/go\/install\)\"
fi

Thanks to Allendarfor the pointers.

感谢Allendar的指点。

回答by FeRD

Just to add to this, Homebrew's --prefixmode has been enhanced here in the far-flung future of 2020 (or maybe it was always this way), so that it now takes a package name as an argument. Meaning locating those "keg-only" packages which aren't linked into standard paths is as easy as:

除此之外,Homebrew 的--prefix模式在 2020 年遥远的未来在这里得到了增强(或者可能一直都是这样),因此它现在将包名称作为参数。这意味着定位那些未链接到标准路径的“仅桶装”包非常简单:

$ brew --prefix ffmpeg
/usr/local/opt/ffmpeg