Loop on Unix, базовый калькулятор геометрических фигур - PullRequest
0 голосов
/ 06 мая 2018

Я пытаюсь создать калькулятор геометрических фигур, и я застрял в цикле.Я использую меню в сценарии оболочки, и пока что мне не повезло.

Может кто-нибудь помочь мне с этим?

Текущий сценарий:

#!/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;;

Как вывидите, я понятия не имею, как остановить цикл и "выйти".

Спасибо!

1 Ответ

0 голосов
/ 06 мая 2018
#!/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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...