In: Computer Science
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
Here is a script that takes Celsius from the user and then converts Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
Formula:
Celsius to Fahrenheit - (0°C × 9/5) + 32 = 32°F
Fahrenheit to Celsius - (32°F − 32) × 5/9 = 0°C
Celsius to Kelvin - 0°C + 273.15 = 273.15K
Fahrenheit to Kelvin - (32°F − 32) × 5/9 + 273.15 = 273.15K
echo
echo command in linux is used to display line of text/string that are passed as an argument
read
read command in Linux system is used to read from a file descriptor
#!/bin/bash
functionn () {
echo -n "Enter temperature (C) : "
read tc
tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
tk=$(echo "scale=2;$tc + 273.15" |bc)
echo "$tc Celsius = $tf Fahrenheit"
echo "$tc Celsius = $tk Kelvin"
tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
tkk=$(echo "scale=2;(5/9)*($tf-32) + 273.15"|bc)
echo "$tf Fahrenheit = $tc Celsius"
echo "$tf Fahrenheit = $tkk Kelvin"
}
functionn
Output:
If you have any doubts, leave a comment below before rating. I'll be happy to assist you further.
Do UPVOTE this as I have put a lot of EFFORT in answering this question. It really helps me.