我想安装brew配方或升级它如果已经使用bash安装。
只有在未安装配方的情况下,该命令才会返回非零退出代码。
PS。如果已安装brew install xxx
,则应注意xxx
返回错误代码。
答案 0 :(得分:1)
背景:https://github.com/Homebrew/legacy-homebrew/issues/30939
我也需要这个。这似乎有效。
#!/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
用法:
> 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
>