首页 > 系统服务 > 详细

linux shell之函数相互调用

时间:2021-04-09 23:39:00      阅读:23      评论:0      收藏:0      [点我收藏+]

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.

./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.

linux shell之函数相互调用

原文:https://www.cnblogs.com/zhudaheng123/p/14638220.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!