apt yum 傻傻分不清楚

最近常遇到centos. 也沒什麼, 用uname或者lsb_relead就知道是哪個distribution.

但是在寫bash的時候就比較麻煩了.

以下是幾個判斷的方式.

參考自 https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh

identify_the_operating_system_and_architecture() {
        if [[ "$(uname)" == 'Linux' ]]; then
                case "$(uname -m)" in
                        'i386' | 'i686')
                                MACHINE='32'
                                ;;
                        'amd64' | 'x86_64')
                                MACHINE='64'
                                ;;
                        'armv5tel')
                                MACHINE='arm32-v5'
                                ;;
                        'armv6l')
                                MACHINE='arm32-v6'
                                grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
                                ;;
                        'armv7' | 'armv7l')
                                MACHINE='arm32-v7a'
                                grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
                                ;;
                        'armv8' | 'aarch64')
                                MACHINE='arm64-v8a'
                                ;;
                        'mips')
                                MACHINE='mips32'
                                ;;
                        'mipsle')
                                MACHINE='mips32le'
                                ;;
                        'mips64')
                                MACHINE='mips64'
                                ;;
                        'mips64le')
                                MACHINE='mips64le'
                                ;;
                        'ppc64')
                                MACHINE='ppc64'
                                ;;
                        'ppc64le')
                                MACHINE='ppc64le'
                                ;;
                        'riscv64')
                                MACHINE='riscv64'
                                ;;
                        's390x')
                                MACHINE='s390x'
                                ;;
                        *)
                                echo "error: The architecture is not supported."
                                exit 1
                                ;;
                esac
                if [[ ! -f '/etc/os-release' ]]; then
                        echo "error: Don't use outdated Linux distributions."
                        exit 1
                fi
                # Do not combine this judgment condition with the following judgment condition.
                ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
                ### Refer: https://github.com/v2fly/fhs-install-v2ray/issues/84#issuecomment-688574989
                if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
                        true
                elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
                        true
                else
                        echo "error: Only Linux distributions using systemd are supported."
                        exit 1
                fi
                if [[ "$(type -P apt)" ]]; then
                        PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
                        PACKAGE_MANAGEMENT_REMOVE='apt purge'
                        package_provide_tput='ncurses-bin'
                elif [[ "$(type -P dnf)" ]]; then
                        PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
                        PACKAGE_MANAGEMENT_REMOVE='dnf remove'
                        package_provide_tput='ncurses'
                elif [[ "$(type -P yum)" ]]; then
                        PACKAGE_MANAGEMENT_INSTALL='yum -y install'
                        PACKAGE_MANAGEMENT_REMOVE='yum remove'
                        package_provide_tput='ncurses'
                elif [[ "$(type -P zypper)" ]]; then
                        PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
                        PACKAGE_MANAGEMENT_REMOVE='zypper remove'
                        package_provide_tput='ncurses-utils'
                elif [[ "$(type -P pacman)" ]]; then
                        PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
                        PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
                        package_provide_tput='ncurses'
                else
                        echo "error: The script does not support the package manager in this operating system."
                        exit 1
                fi
        else
                echo "error: This operating system is not supported."
                exit 1
        fi
}

另外抓是否root權限

check_if_running_as_root() {
        # If you want to run as another user, please modify $UID to be owned by this user
        if [[ "$UID" -ne '0' ]]; then
                echo "WARNING: The user currently executing this script is not root. You may encounter the insufficient privilege error."
                read -r -p "Are you sure you want to continue? [y/n] " cont_without_been_root
                if [[ x"${cont_without_been_root:0:1}" = x'y' ]]; then
                        echo "Continuing the installation with current user..."
                else
                        echo "Not running with root, exiting..."
                        exit 1
                fi
        fi
}

發佈留言