目录

有用的Bash编程例子

目录
警告
本文最后更新于 2017-06-09,文中内容可能已过时。

持续更新 Bash 小技巧

技巧
检测程序是否已安装
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/bash
check_software(){
    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
bash check_software.sh
Checking vim...ok!
Checking git...ok!
Checking tmux...ok!
Checking npm...ok!

相关内容