# 有用的Bash编程例子

<!--more-->

持续更新 `Bash` 小技巧

{{< admonition tip >}}
检测程序是否已安装
{{< /admonition >}}

```bash
#!/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
```

运行结果
```shell
bash check_software.sh
Checking vim...ok!
Checking git...ok!
Checking tmux...ok!
Checking npm...ok!
```


---

> 作者: 泛福轩  
> URL: http://hugo.spiedeman.top/posts/bash/bash-tips/  

