linuxshell之函数相互调⽤vi function9.sh
#!/bin/bash
#函数执⾏显⽰输⼊参数的平⽅
square()
{
echo "Please input the num:"
read num1
let "squ = num1 * num1"
echo "Square of $num1 is $squ."
}
#函数执⾏显⽰输⼊参数的⽴⽅
cube()
{
echo "Please input the num: "
read num2
let "c = num2 * num2 * num2"
echo "Cube of $num2 is $c."
}
#函数执⾏显⽰输⼊参数的幂次⽅
power()
{
echo "Please input the num:"
read num3
echo "Please input the power:"
read p
let "temp = 1"
for((i = 1; i <= $p; i++))
do
let "temp=temp*num3"
done
echo "power $p of $num3 is $temp."
}
#选择调⽤的函数
choice()
{
echo "Please input the choice of operate(s for square; c for cube and p for power):"
read char
#决断输⼊的参数是哪个,然后根据输⼊的不同执⾏不同的函数
case $char in
s)
square;; #执⾏平⽅函数
c)
cube;; #执⾏⽴⽅函数
p)
power;; #执⾏幂运算
*)
echo "What you input is wrong!" ;;
esac
}
#调⽤函数choice
choice
./function9.sh
Please input the choice of operate(s for square; c for cube and p for power):
s
Please input the num:
3
Square of 3 is 9.
./function9.sh
Please input the choice of operate(s for square; c for cube and p for power): c
Please input the num:
4
Cube of 4 is 64.linux执行shell命令
./function9.sh
Please input the choice of operate(s for square; c for cube and p for power): p
Please input the num:
3
Please input the power:
4
power 4 of 3 is 27.
./function9.sh
Please input the choice of operate(s for square; c for cube and p for power): 3
What you input is wrong!
./function9.sh
Please input the choice of operate(s for square; c for cube and p for power): p
Please input the num:
3
Please input the power:
4
power 4 of 3 is 81.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。