交互式自动化脚本模板

作为一个运维人员,免不了会做很多自动化的脚本,服务器安装的自动化,服务环境搭建自动化,服务启动关闭自动化,数据备份恢复自动化等等。 有部分是直接放置在crontab里面按照时间来定运行周期的,还有一部分是手动操作的,对于手动操作这部分,自认最好是交互式的,根据初期定义的功能类型,根据选择的需求完成自动化的部署和实现。 下面就是工作中用的两种比较普遍的交互式自动化脚本的大致框架。 第一种:case函数是实现的交互式
#!/usr/bin/env bash 
#===============================================================================
#          FILE: deploy.sh
#         USAGE: ./deploy.sh 
#   DESCRIPTION: Interactive automated deployment script
#       OPTIONS: ---
#  REQUIREMENTS: ---
#         NOTES: ---
#        AUTHOR: Eric Wu, [email protected]
#       CREATED: 01/25/2014 11:44
#      REVISION:  ---
#===============================================================================

function example001() {
	echo "example001"
}

function example002() {
	echo "example002"
}

function example003() {
	echo "example003"
}

echo " ----------------------------------------"
echo " "
echo "Please insert function Number:"
echo " "
echo "  [1] function001 "
echo "  [2] function002 "
echo "  [3] function003 "
echo " "
echo " ----------------------------------------"
read Number;

case "$Number" in
    1)
        example001 && exit 0;
		;;
	2)
        example002 && exit 0;
		;;
	3)
        example003 && exit 0;
		;;
    *)
        echo $"Usage: $0 {function001|function002|function003}"
        exit 2
esac
  git脚本下载地址: https://github.com/mesopodamia/ShellScript/blob/master/deploy_case.sh 第二种:dialog函数是实现的交互式
#!/usr/bin/env bash 
#===============================================================================
#                 FILE: deploy.sh
#                USAGE: ./deploy.sh 
#   DESCRIPTION: Interactive automated deployment script
#          OPTIONS: ---
#  REQUIREMENTS: ---
#                NOTES: ---
#               AUTHOR: Eric Wu, [email protected]
#          CREATED: 01/25/2014 11:44
#         REVISION:  ---
#===============================================================================

command -v dialog
if [[ $? != 0 ]]; then
 	yum install -y dialog
fi 	

function example001() {
        echo "example001"
}

function example002() {
        echo "example002"
}

function example003() {
        echo "example003"
}

if dialog --title "Auto Deploy System" --radiolist "Choose one function" 20 60 14 1 "function001" on 2 "function002" off 3 "function003" off 2>output; then
        select=`cat output`
        if [ $select -eq 1 ]; then
                example001
        elif [ $select -eq 2 ]; then
                example002
        elif [ $select -eq 3 ]; then
                example003
        else
                echo "Your select is wrong!!! please try again!"
   fi
fi
git脚本下载地址: https://github.com/mesopodamia/ShellScript/blob/master/deploy_dialog.sh

你可能感兴趣的:(shell,脚本)