Linux shell 脚本判断应用是否存在并安装

6 min read
check_cmd () { command -v "$1" &>/dev/null; }

if ! check_cmd tar; then
    echo "tar: command not found, installing..."
    if check_cmd yum; then
        yum install -y tar
    elif check_cmd apt-get; then
        apt-get install -y tar
    elif check_cmd dnf; then
        dnf install -y tar
    else
        echo "Error: Unable to install tar"; exit 1
    fi
fi