How can I implement install-or-upgrade for brew recipes?

别等时光非礼了梦想. 提交于 2019-12-24 00:24:00

问题


I want to install a brew recipe or upgrade it if is already installed using bash.

The command is supposed to return a non zero exit code only if at the end the recipe is not installed.

PS. One should remark the brew install xxx return error code if xxx is already installed.


回答1:


Background: https://github.com/Homebrew/legacy-homebrew/issues/30939

I just needed this also. This seems to work.

#!/usr/bin/env bash

package=$1
pkg_installed=false
pkg_updated=false
verbose=true

# TODO: ensure valid input

brew update >/dev/null 2>&1
list_output=`brew list | grep $package`
outdated_output=`brew outdated | grep $package`

# now enable error checking
set -e

if [[ ! -z "$list_output" ]]; then
    pkg_installed=true
    $verbose && echo "package is installed"
    if [[ -z "$outdated_output" ]]; then
        pkg_updated=true
        $verbose && echo "package is updated"
    else
        $verbose && echo "package is not updated. updating..."
        brew upgrade $package
    fi
else
    $verbose && echo "package is not installed. installing..."
    brew install $package
fi

Usage:

> brew outdated
jemalloc (4.3.0) < 4.3.1
terraform (0.7.9) < 0.7.10
> brew_install_or_upgrade.sh jemalloc; echo $?
package is installed
package is not updated. updating...
==> Upgrading 1 outdated package, with result:
jemalloc 4.3.1
==> Upgrading jemalloc
==> Downloading https://homebrew.bintray.com/bottles/jemalloc-4.3.1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jemalloc-4.3.1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/jemalloc/4.3.1: 16 files, 1.4M
0
> brew_install_or_upgrade.sh jemalloc; echo $?
package is installed
package is updated
0
> 


来源:https://stackoverflow.com/questions/39037601/how-can-i-implement-install-or-upgrade-for-brew-recipes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!