废话不多说,直接上代码,基本实现加减乘除功能:
#!/bin/bash # author by alsww # date : 2016.02.03 # mail : alsww@qq.com # blog : alsww.blog.51cto.com print_info(){ printf "请输入数字:\n" } print_err_num(){ printf "请输入正确的数字!\n" } print_err_fuhao(){ printf "请输入正确的运算符号(+ - * /):\n" } while : do read -p "请输入第一位数字:" num1 echo $num1|grep -q ‘^[-]\?[0-9]\+$‘ && break || print_err_num done while : do read -p "请输入一个运算符(形如 :+ - * /):" ysf if [ "$ysf" != "+" ]&&[ "$ysf" != "-" ]&&[ "$ysf" != "*" ]&&[ "$ysf" != "/" ] then print_err_fuhao else break fi done while : do read -p "请输入第二位数字:" num2 echo $num2|grep -q ‘^[-]\?[0-9]\+$‘ && break || print_err_num done echo "运算结果为:${num1}${ysf}${num2}=$((${num1}${ysf}${num2})) "
效果:
本文出自 “苦咖啡's运维之路” 博客,请务必保留此出处http://alsww.blog.51cto.com/2001924/1740963
原文地址:http://alsww.blog.51cto.com/2001924/1740963