vi function4.sh
#!/bin/bash
#该函数实现将n的值减半
half()
{
let "n = $1" #将参数传递给n
let "n = n/2" #让n的值 减半
echo "in the function half() n is $n"
}
#函数调用
let "m = $1"
echo "Before the function half() is called, m is $m" #显示函数调用前m值
half $m #显示函数调用时m值
echo "After the function half() is called m is $m" #显示函数调用后m值
./function4.sh 10
Before the function half() is called, m is 10
in the function half() n is 5
After the function half() is called m is 10
function5.sh用于实现两数加、减、乘、除四则运算
vi function5.sh
#!/bin/bash
#函数实现两数加、减、乘、除四则运算
count()
{
#判断参个数是否不等于3,不等于3表示输入参数错误
if [ $# -ne 3 ]
then
echo "The number of arguments is not 3! "
fi
let "s = 0"
case $2 in
+) #加操作
let "s = $1 + $3"
echo "$1 + $3 = $s";;
-) #减操作
let "s = $1 - $3"
echo "$1 - $3 = $s";;
\*) #乘操作
let "s = $1 * $3"
echo "$1 * $3 = $s";;
\/) #除操作
let "s = $1 / $3"
echo "$1 / $3 = $s";;
*)
echo "What you input is wrong! ";;
esac
}
#提示输入
echo "Please type your word: (eg. 1 + 1 )"
#读取输入的参数
read a b c
count $a $b $c
测试,忽略
#!/bin/bash
#用于显示参数值
ind_func()
{
echo " $1"
}
#设置间接参数
parameter=ind_arg
ind_arg=Hello
#显示直接的参数
ind_func "$parameter"
#显示间接的参数
ind_func "${!parameter}"
echo "********************"
#改变ind_arg值后的情况
ind_arg=World
ind_func "$parameter"
ind_func "${!parameter}"
./function6.sh
ind_arg
Hello
********************
ind_arg
World