shell函数编程
myfunction(){
echo "hello myfunction";
let a=5+4
return $a
}
mysum(){
let b=1+10
return $b
}
echo "*******";
myfunction
#mysum
echo "获取函数的返回值$?" #获取函数返回值的特有方式,挨着谁近就获取谁的值。
参数函数定义
myfunction(){
#与不带参的函数的写法是一样的,不同的是函数体内的写法
#通过 ${n}的方式获取参数,$# 获取所有参数的个数
#$* 获取所有参数的字符串形式
}
实例:
myfunction(){
echo "hello myfunction";
let a=${1}+${2}
echo "参数的个数是$#"
echo "传入的参数为 $*"
return $a
}
echo "*******";
myfunction 4 6 #调用函数的写法
echo "获取函数的返回值$?"
输出结果为:
*******
hello myfunction
参数的个数是2
传入的参数为 4 6
获取函数的返回值10
myfunction(){
echo "hello myfunction";
let a=5+4
return $a
}
mysum(){
let b=1+10
return $b
}
echo "*******";
myfunction
#mysum
echo "获取函数的返回值$?" #获取函数返回值的特有方式,挨着谁近就获取谁的值。
参数函数定义
myfunction(){
#与不带参的函数的写法是一样的,不同的是函数体内的写法
#通过 ${n}的方式获取参数,$# 获取所有参数的个数
#$* 获取所有参数的字符串形式
}
实例:
myfunction(){
echo "hello myfunction";
let a=${1}+${2}
echo "参数的个数是$#"
echo "传入的参数为 $*"
return $a
}
echo "*******";
myfunction 4 6 #调用函数的写法
echo "获取函数的返回值$?"
输出结果为:
*******
hello myfunction
参数的个数是2
传入的参数为 4 6
获取函数的返回值10