shell脚本之判断不同的系统间软件的安装方法

示例一:通过判断命令类型选择不同系统的安装方式

#!/bin/bash
command_exists () {
    type "$1" &> /dev/null;
}

install_curl () {
    if command_exists apt-get; then
        echo "apt-get -y update
apt-get -y -q install curl"
    elif command_exists yum; then
        echo "yum -y install curl"
    fi
    if ! command_exists curl;then
        echo "command curl not found"
        exit 1;
    fi
}

install_curl

        如果是debian系,就用apt-get;如果是CentOS系,就用yum。type命令用于判断指定的命令类型,以选择不同的安装方式。

示例二:判断是否是root用户安装

#!/bin/bash
root_checking () {
    if [ ! $( id -u ) -eq 0 ]; then
        echo "请使用root权限操作"
        exit 1;
    fi
}

root_checking

示例三:自主选择安装软件的方式

        同一个项目可以选择用docker方式安装,也可以使用rpm或者deb的方式安装。

#!/bin/bash

read_install_method () {
    echo "选择Y使用docker安装。选择N使用RPM/DEB安装。"
    echo "注意事项"
    echo "帮助文档连接"
    read -p "Install with Docker [Y/N/C]? " choice
    case "$choice" in
        y|Y )
            DOCKER="true";
        ;;
        n|N )
            DOCKER="false";
        ;;
        c|C )
            exit 0;
        ;;
         * )
            echo "Please, enter Y, N or C to cancel";
        ;;
     esac
    if [ "$DOCKER" == "" ]; then
        read_install_method;
    fi

}

read_install_method

if [ "$DOCKER" == "true" ]; then
    echo "用docker方式安装"
else
    echo "用rpm或deb方式安装"
fi
posted @ 2023-02-02 11:37  潇湘神剑  阅读(206)  评论(0编辑  收藏  举报