当前位置: 技术问答>linux和unix
有段关于计算乘除的shell脚本出了点问题,大家帮我改改
来源: 互联网 发布时间:2016-02-23
本文导语: #!/bin/bash echo "please enter the number and the operator" read x read o read y if [ $o = + ] then echo $[ $x + $y ] elif [ $o = - ] then echo $[ $x - $y ] elif [ $o = * ] then echo $[ $x * $y ] elif [ $o = / ] then echo $[ $x...
#!/bin/bash
echo "please enter the number and the operator"
read x
read o
read y
if [ $o = + ]
then
echo $[ $x + $y ]
elif [ $o = - ]
then
echo $[ $x - $y ]
elif [ $o = * ]
then
echo $[ $x * $y ]
elif [ $o = / ]
then
echo $[ $x / $y ]
fi
exit 0
以上那段程序加减都行,就乘除出了问题,乘除我都加了转义字符了啊,怎么还出现问题啊
请大家帮我改改
echo "please enter the number and the operator"
read x
read o
read y
if [ $o = + ]
then
echo $[ $x + $y ]
elif [ $o = - ]
then
echo $[ $x - $y ]
elif [ $o = * ]
then
echo $[ $x * $y ]
elif [ $o = / ]
then
echo $[ $x / $y ]
fi
exit 0
以上那段程序加减都行,就乘除出了问题,乘除我都加了转义字符了啊,怎么还出现问题啊
请大家帮我改改
|
建议程序改造如下,已经在linux下测试通过:
#!/bin/bash
echo "please enter the number and the operator"
read x
read o
read y
case $o in
+)
z=`expr $x + $y`;;
-)
z=`expr $x - $y`;;
*)
z=`expr $x * $y`;;
/)
z=`expr $x / $y`;;
esac
echo $z
#!/bin/bash
echo "please enter the number and the operator"
read x
read o
read y
case $o in
+)
z=`expr $x + $y`;;
-)
z=`expr $x - $y`;;
*)
z=`expr $x * $y`;;
/)
z=`expr $x / $y`;;
esac
echo $z
|
像加和减可以用
((x++))
((x--))
非常的方便
((x++))
((x--))
非常的方便
|
按下面修改
echo "please enter the number and the operator"
read x
read o
read y
if [ $o = + ]
then
echo $[ $x + $y ]
elif [ $o = - ]
then
echo $[ $x - $y ]
elif [ $o = "*" ]
then
echo $[ $x * $y ]
elif [ $o = "/" ]
then
echo $[ $x / $y ]
fi
exit 0
echo "please enter the number and the operator"
read x
read o
read y
if [ $o = + ]
then
echo $[ $x + $y ]
elif [ $o = - ]
then
echo $[ $x - $y ]
elif [ $o = "*" ]
then
echo $[ $x * $y ]
elif [ $o = "/" ]
then
echo $[ $x / $y ]
fi
exit 0