Я хочу написать сценарий, который предложит пользователю ввести два числа, представляющих ширину и высоту прямоугольника в сантиметрах, и выведет площадь прямоугольника как в квадратных метрах, так и в квадратных дюймах (один дюйм = 2,54 сантиметра).
Я считаю, что это должно быть относительно просто, но я не могу прийти к рабочему выводу.
#!/bin/sh
read -p "Enter the width and height of rectangle in meters: " width height
sqm=$(echo "$width * $height" | bc -l)
sqin=$(echo "$sqm * 1550" | bc -l)
echo "Area of the rectangle is: $sqm Square Meters or $sqin Square Inches."
(К вашему сведению, 1 квадратный метр равен 1550 квадратным дюймам. Я знаю это, потому что Google мне так говорит.)
Образец выполнения:
$ ./area.sh
Enter the width and height of rectangle in meters: 3.5 4.5
Area of the rectangle is: 15.75 Square Meters or 24412.50 Square Inches.
После исправления одной или двух опечаток в коде, который вы привели в своем комментарии выше, он должен выглядеть так:
#!/bin/sh
echo "Enter the width and height of rectangle:"
read width
read height
echo "Area of the rectangle is:"
expr $width \* $height
Результат:
$ ./tst.sh
Enter the width and height of rectangle:
3
4
Area of the rectangle is:
12
Итак, где же проблема? ;)
#!/bin/sh
read -r -p "please enter width of rectangle: " W
read -r -p "please enter height of rectangle: " H
AREA=`echo "$W $H" | awk '{area=$1*$2; print area}'`
echo "Area of the rectangle is:$AREA"
Откройте Терминал и введите следующее:
touch area.py
chmod 700 area.py
Вставьте это в area.py
#!/usr/bin/env python3
W = input('Enter the width of the rectangle: ')
L = input('Enter the length of the rectangle: ')
A = float(W) * float(L)
print(f'The area is {(A)}')
При запуске программы вам будет предложено ввести длину и ширину, и компьютер вернет площадь.
Извините за сообщение выше. У меня не было возможности удалить или отредактировать свой старый пост, поэтому я просто разместил новый. Я понял, что вам нужны инструкции для оболочки, а не для Python. На всякий случай выложу инструкцию к обоим.
Откройте Терминал и введите следующее:
touch area.sh&&chmod 700 area.sh
Вставьте это вarea.sh
#!/bin/sh
echo 'Enter the width of the rectangle'
read W
echo 'Enter the length of the rectangle'
read L
echo "The area of the rectangle is $((W * L))"
Откройте Терминал и введите следующее:
touch area.py&&chmod 700 area.py
Вставьте это вarea.py
#!/usr/bin/env python3
W = input('Enter the width of the rectangle: ')
L = input('Enter the length of the rectangle: ')
print(f'The area of the rectangle is {float(W)*float(L)}')
Надеюсь, это поможет!