|
我的书中给的函数的定义:
function function_name { commands ; commands ; }
而我在很多以及这本书中列的例子中,特别是这个例子:
#!/bin/bash
#scriptname:do_increment
#there is an different method of declaring a function
increment () {
local num; #sum is known only in this function
let "sum=$1+1"
return $sum #return the value of sum to the script
}
echo -n "the sum is "
increment 5
echo $?
echo "the value of sum is $sum"
首先它用的好像不是所列的那种样式,其次,这个函数内部有的有分号,有的无分号,再次,在bash下运行这个程序的结果不是:
the sum is 6
the value of sum is
而是
the sum is 6
the value of sum is 6
在这个函数里不是分明声明了sum是local吗?而为什么这里用的时候结果又不如书中所说呢?
请大侠指导一下,谢谢。 |
|