linux-tutorial/codes/shell/demos/function/function-demo.sh

43 lines
846 B
Bash
Raw Normal View History

2019-03-01 14:43:49 +08:00
#!/usr/bin/env bash
2019-10-10 08:56:31 +08:00
calc() {
PS3="choose the oper: "
select oper in + - \* / # 生成操作符选择菜单
do
echo -n "enter first num: " && read x # 读取输入参数
echo -n "enter second num: " && read y # 读取输入参数
exec
case ${oper} in
"+")
return $((${x} + ${y}))
;;
"-")
return $((${x} - ${y}))
;;
"*")
return $((${x} * ${y}))
;;
"/")
return $((${x} / ${y}))
;;
*)
echo "${oper} is not support!"
return 0
;;
esac
break
done
2019-03-01 14:43:49 +08:00
}
2019-10-10 08:56:31 +08:00
2019-03-01 14:43:49 +08:00
calc
echo "the result is: $?" # $? 获取 calc 函数返回值
# $ ./function-demo.sh
# 1) +
# 2) -
# 3) *
# 4) /
# choose the oper: 3
# enter first num: 10
# enter second num: 10
# the result is: 100