您的位置:首页 > 运维架构 > Shell

shell脚本编程实现计算器功能并根据选择将其保存于文件中

2017-07-07 23:56 726 查看
#########################################################################
# File Name: project.sh
# Author: lixiaogang
# mail: 2412799512@qq.com
# Created Time: 2017年07月07日 星期五 19时13分07秒
#########################################################################

#!/bin/bash
#  编程方面比Windows批处理强大很多,无论是在循环、运算
# 不适应需要处理大任务的数学操作,尤其是浮点运算,精确运算,或者复杂的算术运

echo
echo -e "\e[1;35m ^_^  Welcome to use  ^_^   \e[0m"
echo -e "\e[1;35m please enter your choice \e[0m"

function add(){     /* 加法功能 */
echo -ne "\e[1;35m Please enter the first number:  \e[0m"
read var1
echo -en "\e[1;35m Please enter the second number: \e[0m"
read var2
echo -e "\e[1;35m The two numbers are ${var1} and ${var2}   \e[0m"
return  $((${var1}+${var2}))
}

function sub(){     /* 减法功能 */
echo -ne "\e[1;36m Please enter the first number:  \e[0m"
read var1
echo -ne "\e[1;36m Please enter the second number: \e[0m"
read var2
echo -e "\e[1;36m The two numbers are ${var1} and ${var2}   \e[0m"

if test ${var1} -gt ${var2}
then
return  $((${var1}-${var2}))
elif [ ${var1} -le ${var2} ]
then
return -$((${var2}-${var1}))
elif [ ${var1} -eq ${var2}]
then
return 0
else
echo "^_^ ^_^ ^_^"
fi
}

function mul(){     /* 乘法功能 */
echo -ne "\e[1;35m Please enter the first number: \e[0m"
read var1
echo -ne "\e[1;35m Please enter the second number:\e[0m"
read var2
echo -e "\e[1;36m The two numbers are ${var1} and ${var2} \e[0m"
return $((${var1}*${var2}))
}

function div(){     /* 除法功能 */
echo -ne "\e[1;35m Please enter the first number: \e[0m"
read var1
echo -ne "\e[1;35m Please enter the second number:\e[0m"
read var2
echo -e "\e[1;36m The two numbers are ${var1} and ${var2} \e[0m"
return $((${var1}/${var2}))
}

function rdwr(){        /* 文件操作功能 */
# 0-255 can only be integer  can't return string
array=("yes" "no")
echo -ne "\e[1;32m 数组元素个数有: ${#array[*]}个,分别是:\e[0m"
for value in ${array[*]}    #等价于 for var in ${array[@]}
do
echo -ne "\e[1;36m  \"$value\"  \e[0m"
done

echo
read input   # Debugging 40 minutes. Oh my god. it's crazy.
if [ ${input}=${array[0]} ]
then
# yes
touch lxg.inf
return 0
elif [[ ${input}=${array[1]} ]]
then
echo -e "\e[1;32m  You chose no \e[0m"
else
echo -e "\e[1;32m  Input Error! \e[0m"
fi
}

while true  # while command(通常为测试条件)
do

printf "\n%s\n" " 1.calculator 2.rdwr_file"
list="Please enter your operator:   \" + - * / \" "
read val
if [ ${val} -eq 1 ]
then
#   -n表示echo终端打印不换行 -e使用转义字符
echo -n " You choice is: ${val}"
echo
echo -e "\e[1;32m  ${list}  \e[0m"
fmat="Ubuntu> "
echo -en "\e[1;31m $fmat \e[0m"
read operator

#   case 多分支选择结构 支持 a|b|c|d)
case ${operator} in
+)
add
readonly ret=$?
echo "sum is: ${ret}"
echo -e "\e[1;31m Do you want to save the value? \"yes\" or \"no\" \e[0m"
rdwr
file=$?  #最后运行的命令的结束代码(返回值)
if test $file -eq 0
then
DATE=`date`
echo $DATE >> lxg.inf
echo $ret >> lxg.inf
fi
;;
-)
sub
ret=$?
echo "sub is: ${ret}"
echo -e "\e[1;31m Do you want to save the value?  \e[0m"
rdwr
file=$?
if test $file -eq 0
then
DATE=`date`
echo $DATE >> lxg.inf
echo $ret >> lxg.inf
fi
;;
\*)
mul
ret=$?
echo "mul is: ${ret}"
echo -e "\e[1;31m Do you want to save the value?  \e[0m"
rdwr
file=$?
if test $file -eq 0
then
DATE=`date`
echo $DATE >> lxg.inf
echo $ret >> lxg.inf
fi
;;
/)
div
ret=$?
echo "div is: ${ret}"
echo -e "\e[1;31m Do you want to save the value?  \e[0m"
rdwr
file=$?
if test $file -eq 0
then
DATE=`date`
echo $DATE >> lxg.inf
echo $ret >> lxg.inf
fi
;;
*)  #若没有找到匹配模式,*来匹配改值
echo -e"\e[1;31m input ${operator} error. \e[0m"
break
;;
esac

elif test ${val} -eq 2
then
echo -n "You choice is: ${val}"
#   rdwr
else
echo -e "\e[1;32m You choice error. \e[0m"
fi

done

echo "Thank you. Bye"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: