869   Linux Shell

最近需要用shell循环执行一些检查工作,于是写了个循环执行函数的脚本,代码如下:
主要函数:

# 用于判断是否要重复执行,还是跳过等
function skip_check{}
# 用于循环执行函数
function run_main{}

完整代码:


#!/bin/bash

##
# function start
##

function func_one
{
        echo -e "\n"
        echo -e "This is func_one \n"
        echo -e "\n"
}

function func_two
{
        echo -e "\n"
        echo -e "This is func_two \n"
        echo -e "\n"
}
function func_three
{
        echo -e "\n"
        echo -e "This is func_three \n"
        echo -e "\n"
}

# 用于判断是否要重复执行,还是跳过等
function skip_check
{
        # 获取需要执行的方法名
        local FUNC_CHECK_FUNCTION_NAME="$1"
        local SKIP_CHECK_RETURN="done"

        echo -e "\n"
        echo -e "Next Function: ${FUNC_CHECK_FUNCTION_NAME} \n"
        echo -e "\n"
        echo -e "Input 'done' if done \n"
        echo -e "Input 'back' if back \n"
        echo -e "Input 'skip' if skip \n"
        echo -e "Input 'quit' if quit \n"
        echo -e "\n"
        printf "Your choice : "

        # 捕捉输入
        read FUNC_BREAK_CHECK_READ

        echo -e "\n"

        # 添加X,避免输入空格异常
        if [ ${FUNC_BREAK_CHECK_READ}X == "done"X ]
        then
                echo -e "done ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 1
		elif [ ${FUNC_BREAK_CHECK_READ}X == "skip"X ]
        then
                echo -e "skip ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 2
        elif [ ${FUNC_BREAK_CHECK_READ}X == "back"X ]
        then
                echo -e "back ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 3
        elif [ ${FUNC_BREAK_CHECK_READ}X == "quit"X ]
        then
                echo -e "quit ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 4
        else
                # 如果输入异常,重复执行
				echo -e "Input is not right \n"
                skip_check ${FUNC_CHECK_FUNCTION_NAME}
        fi

}

# 用于循环执行函数
function run_main
{
        # 循环执行的 function 
        local RUN_MAIN_FUNCTION=(
                func_one
                func_two
                func_three
        )

        local FUNC_FUNCTION_NUMBER=${#RUN_MAIN_FUNCTION[@]}
        local FUNC_SKIP_CHECK_RETURN=1
        local ALL_FUNCTION_RESULT


        local FUNC_COUNTER=0
        # 循环执行方法
        while [ ${FUNC_COUNTER} -lt ${FUNC_FUNCTION_NUMBER} ]
        do
				# 判断是否要执行
                skip_check ${RUN_MAIN_FUNCTION[${FUNC_COUNTER}]}
				# 获取判断值
                FUNC_SKIP_CHECK_RETURN=$?

                if [ ${FUNC_SKIP_CHECK_RETURN} -eq 1 ]
                then
						#执行函数
						${RUN_MAIN_FUNCTION[${FUNC_COUNTER}]}
						FUNC_COUNTER=`expr ${FUNC_COUNTER} + 1`
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 2 ]
                then
						# 跳过
						FUNC_COUNTER=`expr ${FUNC_COUNTER} + 1`
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 3 ]
                then
						# 回退
                        if [ ${FUNC_COUNTER} -gt 0 ]
                        then 
                                FUNC_COUNTER=`expr ${FUNC_COUNTER} - 1`
                        fi
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 4 ]
                then 
						# 退出
                        break;
                fi


        done

        echo -e "\n\n"
        echo -e "All of functions had be completed \n"



}


##
# function end
##


##
# main start
##

run_main


##
# main end
##



Leave a Reply

Your email address will not be published. Required fields are marked *