#!/bin/bash CMD_INSTALL="" CMD_UPDATE="" SOFTWARE_UPDATED=0 #########color code############# RED="31m" # Error message GREEN="32m" # Success message YELLOW="33m" # Warning message BLUE="36m" # Info message ###############color echo func################# colorEcho(){ echo -e "\033[${1}${@:2}\033[0m" 1>& 2 } # 判断系统的包管理工具 apt, yum, or zypper getPackageManageTool(){ if [[ -n `command -v apt-get` ]];then CMD_INSTALL="apt-get -y -qq install" CMD_UPDATE="apt-get -qq update" elif [[ -n `command -v yum` ]]; then CMD_INSTALL="yum -y -q install" CMD_UPDATE="yum -q makecache" elif [[ -n `command -v zypper` ]]; then CMD_INSTALL="zypper -y install" CMD_UPDATE="zypper ref" else return 1 fi return 0 } ## 安装所需要的程序,及依赖程序 installDemands(){ COMPONENT=$1 if [[ -n `command -v $COMPONENT` ]]; then return 0 fi getPackageManageTool if [[ $? -eq 1 ]]; then colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually." return 1 fi ### 更新程序引索 if [[ $SOFTWARE_UPDATED -eq 0 ]]; then colorEcho ${BLUE} "正在更新软件包管理..." $CMD_UPDATE SOFTWARE_UPDATED=1 fi colorEcho ${BLUE} "正在安装 ${COMPONENT}.." $CMD_INSTALL $COMPONENT if [[ $? -ne 0 ]]; then colorEcho ${RED} "安装 ${COMPONENT} 失败. 请手动安装该程序." return 1 else colorEcho ${GREEN} "已经成功安装 ${COMPONENT}." fi return 0 } ## 下面只是例子,使用时在主函数中应用之 installDemands "iftop" || return $?