日期:2014-05-16  浏览次数:20934 次

expr: 语法错误
这个我没看出哪错了,求大家掌掌眼,谢谢!!
源码:
#!/bin/sh
add()
{
  a=$1
  b=$2
  z=`expr $a + $b`
  echo "The result is $z"
}
add $1 $2

运行:
qustdjx@qustdjx-K42JZ:~/test$ ./test
expr: 语法错误
The result is 


------解决方案--------------------
探讨
这个我没看出哪错了,求大家掌掌眼,谢谢!!
源码:
#!/bin/sh
add()
{
a=$1
b=$2
z=`expr $a + $b`
echo "The result is $z"
}
add $1 $2

运行:
qustdjx@qustdjx-K42JZ:~/test$ ./test
expr: 语法错误
The result is

------解决方案--------------------
执行的时候没有传递参数,所以错误。
$ ./test 1 2
看看,应该就好了。
为了防止这种错误,前面应该加一个判断,看看$1和$2是否提供了。

C# code
[root@bogon home]# sh -x t2.sh 1 2
+ add 1 2
+ a=1
+ b=2
++ expr 1 + 2
+ z=3
+ echo 'The result is 3'
The result is 3