bash-tips

持续更新 bash 小技巧

  • 检测程序是否已安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

(){
local software=("vim" "git" "tmux" "npm")
for soft in ${software[@]}
do
type $soft 2>&1 > /dev/null # 已安装,则返回零
if [ $? -ne 0 ]; then
echo "ERROR: **$soft** is not installed!"
exit 1
fi
echo "Checking $soft...ok!"
done
}

check_software
1
2
3
4
5
6
运行结果
bash check_software.sh
Checking vim...ok!
Checking git...ok!
Checking tmux...ok!
Checking npm...ok!