Detect if homebrew package is installed

前端 未结 4 1043
梦如初夏
梦如初夏 2021-01-30 04:42

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?

I tried usin

相关标签:
4条回答
  • 2021-01-30 05:33

    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):

    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.

    0 讨论(0)
  • 2021-01-30 05:34
    # 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
    
    0 讨论(0)
  • 2021-01-30 05:39

    Easiest two-liners: Step one, make sure it's installed

    $ realpath . || brew install coreutils
    

    This will print out the realpath of current dir, if not, then it will install it. And it will not fail even realpath not found.

    Step two, call it in your actual code:

    $ realpath ${someDir}
    
    0 讨论(0)
  • 2021-01-30 05:40

    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
    
    0 讨论(0)
提交回复
热议问题