我正在尝试创建一个几何图形的计算器,我陷入了一个循环。我在shell脚本中使用菜单,到目前为止没有运气。
有人可以帮助我吗?
当前脚本是:
#!/bin/bash
echo "Welcome, please enter username:"
read $username
echo "WELCOME $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1), This is a basic calculator of square,circle and rectangle. "
echo "Please choose from 1,2 or 3 to continue: "
echo "1. Rectangle"
echo "2. Circle"
echo "3. Square"
echo "4. Quit"
read choice
if [ $choice -eq 1 ]
then
echo "You choose rectangle"
echo "Enter the height:"
read height
echo "Enter the width:"
read width
area=`echo "$height * $width"|bc`
echo "The area of the rectangle is:"$area
else
if [ $choice -eq 2 ]
then
echo "You choose circle"
echo "Enter the radius of the circle:"
read radius
area1=`echo "3.141 * $radius * $radius"|bc`
echo "The area of the circle is:"$area1
else
if [ $choice -eq 3 ]
then
echo "You choose square"
echo "Enter the width of the square:"
read w
area2=`echo "$w * $w"|bc`
echo "The area of the circle is:"$area2
else
If [ $choice -eq 4 ]
then "You choose quit"
"Quit")
break
;;
*) echo invalid option;;
如您所见,我不知道如何停止循环并“退出”。
谢谢!
答案 0 :(得分:1)
#!/bin/bash
echo "Welcome, please enter username:"
read $username
echo "WELCOME $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1), This is a basic calculator of square,circle and rectangle. "
while true
do
echo "Please choose from 1,2 or 3 to continue: "
echo "1. Rectangle"
echo "2. Circle"
echo "3. Square"
echo "4. Quit"
read choice
if [ $choice -eq 1 ]
then
echo "You choose rectangle"
echo "Enter the height:"
read height
echo "Enter the width:"
read width
area=`echo "$height * $width"|bc`
echo "The area of the rectangle is:"$area
elif [ $choice -eq 2 ]
then
echo "You choose circle"
echo "Enter the radius of the circle:"
read radius
area1=`echo "3.141 * $radius * $radius"|bc`
echo "The area of the circle is:"$area1
elif [ $choice -eq 3 ]
then
echo "You choose square"
echo "Enter the width of the square:"
read w
area2=`echo "$w * $w"|bc`
echo "The area of the circle is:"$area2
elif [ $choice -eq 4 ]
then
break
else
echo "invalid option"
fi
done
我想你想这样做。这将无限循环,直到你按下4。