例如用shell脚本判断是否安装了jq
#判断是否安装 json解析工具“jq”
if [ `command -v jq` ];then
echo 'jq 已经安装'
else
echo 'jq 未安装,开始安装json解析工具'
#安装jq
brew install jq
fi
未安装情况,运行后终端打印
jq 未安装,开始安装json解析工具
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
plantuml pmd
==> Installing dependencies for jq: oniguruma
==> Installing jq dependency: oniguruma
==> Downloading https://homebrew.bintray.com/bottles/oniguruma-6.9.1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/19/1983059c2eae93f1e1ec1bdb65194c64b77182c444588c15085dcdf9095bac2a?__gda__=exp=1556602944~hmac=12e9062a46320ae91213c42d5df89dee54a0da8a1b5f3e91
######################################################################## 100.0%
==> Pouring oniguruma-6.9.1.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/oniguruma/6.9.1: 17 files, 1.3MB
==> Installing jq
==> Downloading https://homebrew.bintray.com/bottles/jq-1.6.mojave.bottle.1.tar.gz
==> Downloading from https://akamai.bintray.com/71/71f0e76c5b22e5088426c971d5e795fe67abee7af6c2c4ae0cf4c0eb98ed21ff?__gda__=exp=1556603025~hmac=6f796aaa7a309d6060b786632a410604e1d6e5f4817e9d9c
######################################################################## 100.0%
==> Pouring jq-1.6.mojave.bottle.1.tar.gz
🍺 /usr/local/Cellar/jq/1.6: 18 files, 1MB
安装后再次运行脚本终端打印
jq 已经安装