#!/bin/bash

#定义最大公约数和最小公约数
GCD_RESULT=""
LCM_RESULT=""

#定义一个相加的函数
function sum() {
 result=$(( num1+num2 )) 
 echo -e "\033[31m 两个数之和为$result \033[0m"
}

#定义一个求最大公约数函数
function divisor() {
 if [ "$num1" -eq "$num2" ];   
 then  
      s_great=$num1
      s_small=$num2
      #export s_great
      #export s_small
 fi  
 if [ "$num1" -gt "$num2" ];   
 then   
     s_great=$num1  
     s_small=$num2  
 else  
     s_great=$num2  
     s_small=$num1  
 fi 
 i=1  
 GCD_RESULT=1  
 greattmp=1  
 smalltmp=1  
 LCM_RESULT=1  
 export GCD_RESULT
 while [ $i -le "$s_small" ]  
 do  
    greattmp=`expr $s_great % $i`  
    smalltmp=`expr $s_small % $i`  
    if [ ${greattmp} -eq 0 ]; then   
        if [ ${smalltmp} -eq 0 ]; then  
            GCD_RESULT=${i}  
        fi  
    fi  
    i=`expr ${i} + 1`  
 done  
 LCM_RESULT=`expr $s_small / $GCD_RESULT`
 LCM_RESULT=`expr $LCM_RESULT \* $s_great`  
 #echo -e "\033[31m 两个数最大公约数为$s_great \033[0m"
} 

function say() {
   divisor
   echo -e "\033[31m 两个数最大公约数为$GCD_RESULT \033[0m"
}


#定义一个求最小公倍数函数
function multiple() {
   divisor
   
   echo -e "\033[31m 两个数最小公倍数为$LCM_RESULT \033[0m"
}

function main() {
  echo "---------------------------"
  echo "请输入你要执行的方法"
  echo "输入1:执行相加函数,返回两数之和"
  echo "输入2:执行最大公约数函数,返回两数最大公约数"
  echo "输入3:执行最小公倍数函数,返回两数最小公倍数"
  echo "输入4:退出"
  read input
  case $input in
  1)
  sum;;
  2)
  say;;
  3)
  multiple;;
  4)
  exit;;
  esac 

}


if [ $# != 2 ];then
   echo "命令格式不对, 命令格式:  sh d.sh num1 num2"
   exit
fi

#根据返回的状态判断输入是否为数字
num=`expr $1 + 0 &>/dev/null`

if [ $? -ne 0 ];then
   echo "num1 必须是数字格式"
   exit
fi

num=`expr $2 + 0 &>/dev/null`

if [ $? -ne 0 ];then
   echo "num2 必须是数字格式"
fi

#显示选择菜单

while true
do
  num1=$1
  num2=$2
  main
done