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