如何最小化此代码,因此不必重复?

时间:2018-06-04 12:49:10

标签: linux bash shell centos

我目前正在开发一个shell脚本,它将充当Multicraft的自动安装程序。

我编写了一个片段,检查操作系统是否为CentOS,然后根据正在使用的CentOS版本决定使用哪些命令。

# Check server is CentOS
if [ ! -f "/etc/redhat-release" ]; then
    echo -e ${COL_RED}"[FATAL]" ${COL_RESET}"This is not a standard CentOS Linux installation!
    Aborting..."
    quit
fi

# Check version of CentOS
OS_VERSION=$(rpm -qa \*-release | grep -Ei "oracle|redhat|centos" | cut -d"-" -f3)

我经常使用此功能的一个领域是启动/启用服务。例如:

if [ "${OS_VERSION}" = "7" ]; then
  systemctl enable mariadb
  systemctl start mariadb
elif [ "${OS_VERSION}" != "7" ]; then
  chkconfig mariadb on
  service mariadb start
else
  echo -e ${COL_RED}"[FATAL] "${COL_RESET}"Unable to derive OS version."
  echo "Aborting..."
  quit
fi

每次我想使用这个功能时,我目前都要重复相同的块,我想知道是否有任何方法可以缩短/最小化这个,以使它看起来更整洁?

0 个答案:

没有答案