当前位置: 首页 > 工具软件 > Ani-Shell > 使用案例 >

shell脚本 + - * / 【笔记】

慕承允
2023-12-01

 脚本  calc.sh

echo "shell脚本:计算加减乘除。文件名称:$0"
echo "输入+,-,x,/"
echo "格式示例:sh calc.sh 10 2 +"

a=$1
b=$2
op=$3

echo \* is *

if [ $op = "+" ];then
 echo $a + $b = `expr $a + $b`
elif [ $op = "-" ];then
 echo $a - $b = `expr $a - $b`
elif [ $op = "x" ];then
 echo $a \* $b = `expr $a \* $b`
elif [ $op = "/" ];then
 echo $a / $b = `expr $a / $b`
else
 echo "operation is not supported!"
fi

结果:

[aaa@bbb sh-dir]$ ./calc.sh 10 2 +
shell脚本:计算加减乘除。文件名称:./calc.sh
输入+,-,x,/
格式示例:sh calc.sh 10 2 +
* is calc.sh first.sh second.sh test.sh third.sh times.sh
10 + 2 = 12
[aaa@bbb sh-dir]$ ./calc.sh 10 2 -
shell脚本:计算加减乘除。文件名称:./calc.sh
输入+,-,x,/
格式示例:sh calc.sh 10 2 +
* is calc.sh first.sh second.sh test.sh third.sh times.sh
10 - 2 = 8
[aaa@bbb sh-dir]$ ./calc.sh 10 2 x
shell脚本:计算加减乘除。文件名称:./calc.sh
输入+,-,x,/
格式示例:sh calc.sh 10 2 +
* is calc.sh first.sh second.sh test.sh third.sh times.sh
10 * 2 = 20
[aaa@bbb sh-dir]$ ./calc.sh 10 2 /
shell脚本:计算加减乘除。文件名称:./calc.sh
输入+,-,x,/
格式示例:sh calc.sh 10 2 +
* is calc.sh first.sh second.sh test.sh third.sh times.sh
10 / 2 = 5

 问题:用  *  表示 乘号  ,作为输入参数时

输入命令:sh calc.sh 10 2 * 

脚本处理 有问题,暂未找到合理解决办法,菜鸟一个。。。,只能用小写 代替

如脚本中  echo \* is *

输出结果:* is calc.sh first.sh second.sh test.sh third.sh times.sh

请大神指教。 

 类似资料: