2. Write a complete bash script to convert input centigrade temperature value to
ID: 3749709 • Letter: 2
Question
2. Write a complete bash script to convert input centigrade temperature value to Fahrenheit temperature.
The conversion formula is °C x 9/5 + 32 = °F
You must loop thru the script until the user enters 1 or 0 (1 = continue, 0 = exit)
Here is a screen dump of an actual run….
$ ./celsius_to_fahrenheit.sh
Celsius to Fahrenheit Converter
===============================
What is the temperature in Celsius?34
Celsius 34 is Fahrenheit 93
Do you want to continue?(1 = yes, 0 = no) 1
Celsius to Fahrenheit Converter
===============================
What is the temperature in Celsius?67
Celsius 67 is Fahrenheit 152
Do you want to continue? (1 = yes, 0 = no) 1
Celsius to Fahrenheit Converter
===============================
What is the temperature in Celsius? 40
Celsius 40 is Fahrenheit 104
Do you want to continue? (1 = yes, 0 = no)
0
Thank You. BYE!
Explanation / Answer
Bash script to convert Centigrade temperature value to Fahrenheit temperature.
-----------------------------------------------------------------------
while :
do
echo "Celsius to Fahrenheit Converter"
echo "==============================="
echo -n "What is the temperature in Celsius?"
read tc
# Formula °C x 9/5 + 32 = °F
tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc) #Calculating temperature in Fahrenheit
echo "Celsius $tc is Fahrenheit $tf"
echo -n "Do you want to continue?(1 = yes, 0 = no)" #Prompting for user choice for Exit or Continue
read a
if [ $a -eq 0 ] #If user choice equals to 0 loop terminates
then
echo "Thank You. BYE!" #If choice is 0 loop terminates and print Thank you Bye!
break
fi #End of if Condition
done #End of while loop
-------------------------------------------------------------------------------------
Thank you,
With regards.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.