shell脚本实现算术运算且输入的不能为非数字的值

#!/bin/bash


c=1

while [ ${c} -eq  1 ]
do
echo "请输入第一个数"
read a
echo "请输入第二个数"
read b

if [[ ${b} != *[!0]* ]] && [[ ${a} != *[!0123456789]* ]] && [ -n "${a}"  ] && [ -n "${b}" ]  ;

then
sleep 1
echo "   "
echo "   "
echo "和为:"`echo $(( ${a} + ${b} ))`
echo "差为:"`echo $(( ${a} - ${b} ))`
echo "积为:"`echo $(( ${a} * ${b}))`
echo "   "
sleep 2
echo -e "\033[33m除数为0,无法进行除法运算以及取余运算!\033[0m"

c=$(( ${c} + 1 ))
elif [[ ${a} != *[!0123456789]* ]] && [[ ${b} != *[!0123456789]* ]] && [ -n "${a}" ] && [ -n "${b}" ] ;

then
sleep 1
echo "  "
echo "  "
echo "和为:"`echo $(( ${a} + ${b} ))`
echo "差为:"`echo $(( ${a} - ${b} ))`
echo "商为:"`echo $(( ${a} / ${b} ))`
echo "积为:"`echo $(( ${a} * ${b}))`
echo "余数为:"`echo  $(( ${a} % ${b}))`

c=$(( ${c} + 1 ))

else

sleep 3

echo "  "

echo -e "\033[31m输入错误,请从新输入正确的值!\033[0m"

echo "  "
fi

done

 

posted @ 2016-12-01 11:14  张老司机  阅读(1008)  评论(0编辑  收藏  举报