当前位置: 技术问答>linux和unix
一段Shell脚本的语法问题
来源: 互联网 发布时间:2017-03-22
本文导语: #!/bin/bash #Reverse Order echo "Reverse Order and print." if [ $# -ne 1 ] then echo "Usage $0 number" echo " I will find reverse order of given number." echo " For e.g. $0 123, I will print 321." exit 1 fi n=$1 rev=0 sd=0 while [ ...
#!/bin/bash
#Reverse Order
echo "Reverse Order and print."
if [ $# -ne 1 ]
then
echo "Usage $0 number"
echo " I will find reverse order of given number."
echo " For e.g. $0 123, I will print 321."
exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev /* 10 + $sd`
n=`expr $n / 10`
echo "$sd $rev $n"
done
echo "Reversed number is $rev"
结果打印如下:
Reverse Order and print.
expr: syntax error
3 872
expr: syntax error
2 87
expr: syntax error
7 8
expr: syntax error
8 0
Reversed number is
为什么执行后会是有语法错误???
|
rev=`expr $rev /* 10 + $sd` 改为
rev=`expr $rev * 10 + $sd`
是运算符出错导致的。
rev=`expr $rev * 10 + $sd`
是运算符出错导致的。
|
`expr $rev /* 10 + $sd` ?