Shell 实现计算器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell 实现计算器相关的知识,希望对你有一定的参考价值。
话不多说,上代码:
#!/bin/env sh
# Author: BlueMiaomiao
# E-mail: xv2017@outlook.com
# GitHub: bluemiaomiao.github.io
# Desc : Simple Computer Design
FIR_NUM=`echo $1 | awk -F ‘+|-|*|/‘ ‘{print $1}‘`
SEC_NUM=`echo $1 | awk -F ‘+|-|*|/‘ ‘{print $2}‘`
OPS_SYM=`echo $1 | sed ‘s/[0-9]//g‘`
if [ "$OPS_SYM" == ‘+‘ ]
then
echo "$FIR_NUM + $SEC_NUM = `expr $FIR_NUM + $SEC_NUM`"
elif [ "$OPS_SYM" == ‘-‘ ]
then
echo "$FIR_NUM - $SEC_NUM = `expr $FIR_NUM - $SEC_NUM`"
elif [ "$OPS_SYM" == ‘*‘ ]
then
echo "$FIR_NUM * $SEC_NUM = `expr $FIR_NUM * $SEC_NUM`"
elif [ "$OPS_SYM" == ‘/‘ ]
then
if [ "$SEC_NUM" == 0 ]
then
echo ‘Params error.‘
else
echo "$FIR_NUM ÷ $SEC_NUM = `expr $FIR_NUM / $SEC_NUM`"
fi
else
echo ‘Syntax error.‘
fi
以上是关于Shell 实现计算器的主要内容,如果未能解决你的问题,请参考以下文章