我正在学习在Shell中制作小程序,我正在研究如何定义使用系统计算器(bc -l)的函数。我将展示我想要做的事情:
#!/bin/bash
square (){
I need help here!
}
cube (){
I need help here!
}
rectangle (){
I need help here!
}
exit (){
help
}
I need help here!
echo "Little program that computes the following:"
echo "a) Surface of a square"
echo "b) Volume of a cube"
echo "c) Surface of a rectangle"
echo "e) exit"
echo "Choose the option you want to compute"
read answer
if [ $respuesta == "a" ]
then echo "What's the side of the square"
read l`
在那之后,我现在不知道如何调用我的功能"吃"用户的回答,然后显示计算。事情变得更糟,因为在计算之后我必须询问用户他是否想要继续或退出。如果有人能帮助我,我将非常感激。抱歉我的英语。我不是母语人士。
答案 0 :(得分:0)
这是你在找什么?:
menu="some items to choose from:
A) something
B) something else"
sample () {
a="$1"
b="$2"
c="$3"
printf "c: %s" "$c"
echo "$a+$b" | bc
echo "$a*$b" | bc
echo "$a/$b" | bc
}
add=$(echo "2+2" | bc)
printf "Added: %s\n" "$add"
echo "$menu"
read -p "choose: " choice
case "$choice" in
A) sample "30" "5";;
B) sample "2" "2" "2";;
*) echo "not a choice";;
esac
答案 1 :(得分:0)
以下是使用select语句在BASH中支持的代码。对于方形,立方体,矩形的功能,你可以很好地完成它。
#!/usr/bin/env bash
square (){
echo "I need help here! - square"
}
cube (){
echo "I need help here! - cube"
}
rectangle (){
echo "I need help here! - rectangle"
}
exit (){
echo "help"
}
options=("Surface of a square" "Volume of a cube" "Surface of a rectangle" "exit")
select opt in "${options[@]}"
do
case $opt in
"${options[0]}")
echo "you chose choice 1"
square
;;
"${options[1]}")
echo "you chose choice 2"
cube
;;
"${options[2]}")
echo "you chose choice 3"
rectangle
;;
"${options[3]}")
break
;;
*) echo invalid option;;
esac
done