加减乘除shell运算脚本实例分享

发布时间:2020-06-14编辑:脚本学堂
分享一个加减乘除的shell运算脚本,学习下shell脚本中算术运算的实现方法,需要的朋友参考下。

例子,shell/ target=_blank class=infotextkey>shell脚本进行加减乘除运算。
 

复制代码 代码示例:

#!/bin/bash
#

echo "example: 3 + 5"
read -p "enter:  " a b c
echo $a|grep [a-zA-Z]>/dev/null
if [ $? -ne 0 ];then
echo $c|grep [a-zA-Z]>/dev/null
if [ $? -ne 0 ];then
if [ $b == "+" ]
then
let x=$a+$c
echo "$a + $c = $x"
fi
if [ $b == "-" ]
then
let x=$a-$c
echo "$a - $c = $x"
fi
if [ $b == "x" ]
then
let x=$a*$c
echo "$a x $c = $x"
fi
if [ $b == "/" ]
then
let x=$a/$c
echo "$a / $c = $x"
fi
else
echo "$c is not data"
fi
else
echo "$a is not data"
fi