[root@shell /scripts/day7]# vim count.sh 

#!/usr/bin/env bash
# Author:xiaolang
# Email: 2306126035@qq.com
# Time:2020-11-25  03:57:41
# Name:count.sh
# Version:VI.0
# Discription:To  

# 功能介绍
function echo_info() {
echo " ----------------------------------" 
echo "|这是一个简单的整数计算器,biubiu |" 
echo " ----------------------------------" 
echo
}

# 接受参数
function accept_data(){
    read -p "请输入第$1 个整数:" num
    expr $num + 0 &> /dev/null
    if [ $? -eq 0 ];then
        return 0
    else
        return 1
    fi
}

# 接受输入的操作
function operation(){
echo "-------------------" 
echo "| 1.加法 |" 
echo "| 2.减法 |" 
echo "| 3.乘法 |" 
echo "| 4.除法 |" 
echo "-------------------" 
read -p "请输入您想执行的操作:" choice
expr $choice + 0 &> /dev/null
if [ $? -eq 0  ];then
    if [ $choice -lt 5 -a $choice -ge 1 ];then
        return $choice
    else
        echo "输入超出范围"
    fi
else
    echo "非法输入"
fi
}


function func_menu(){
echo -e "继续:1 \n退出 2"
read -p '输入选项:' inp_num
if [ $inp_num -eq 1 ];then
    return 1
elif [ $inp_num -eq 2 ];then
    return 2
else
    return 3
fi
}

function judge(){
declare -A match=([1]="+" [2]="-" [3]='*' [4]="/")
for i in ${!match[*]}
do
    if [ $i -eq $3 ];then
       res=`echo  "$1 ${match[$i]} $2" | bc `
       echo "$1 ${match[$i]} $2 = $res"
    fi
done
}

function main(){
while true
do
    echo_info
    accept_data 1
    if [ $? -eq 0 ];then
        num_1=$num
        accept_data 2
        if [ $? -eq 0 ];then
            num_2=$num
        else
            echo "必须输入整数!"
            continue
        fi
    else
        echo "必须输入整数!"
        continue
    fi
    operation
    judge $num_1 $num_2 $?
    func_menu
    flag=$?
    if [ $flag -eq 2 ];then
        break
    elif [ $flag -eq 3 ];then
        echo '非法操作'
        func_menu
    elif [ $flag -eq 1 ];then
        echo '再次计算 !'     
    fi

done
}

main