convert Centigrade ,Fahrenheit and Kelvin

Very useful script to convert Centigrade, Fahrenheit and Kelvin ;all three scales for measuring temperature.

Centigrade scale has water freezing at 0 c and boil at 100 c
Fahrenheit scale has water freezing at  32 f and boil at 212 f
Kelvin scale has absolute absence of heat  at 0 k , freezing of water at 273.15 k and boiling at 373.15 k

-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
This script uses following formulas
Converting Centigrade(C) to Fahrenheit(F) :
     F=(C * ( 9/5) )+ 32 
Converting Fahrenheit(F) to Centigrade(C) :
    C=(F-32)*(5/9)
Converting Centigrade(C) to Kelvin(K) :
    K=C+275.15
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to convert centigrade, fahrenheit and kelvin

#checking if any temperature scale is specified
c=$(echo $1 | grep -c "c")
f=$(echo $1 | grep -c "f")
k=$(echo $1 | grep -c "k")


#doing calculations
if (( $c == "1" ));then
   c=$(echo $1 | sed 's/c//g');
   f=$(echo "scale=1;($c*(9/5))+32" | bc -l)
   k=$(echo "scale=2;$c+273.15"|bc -l)
   echo -e "\n\e[1;30m=$f f & $k k\e[0m\n";
elif (( $f == "1" ));then
   f=$(echo $1 | sed 's/f//g');
   c=$(echo "scale=1;(($f-32)*5)/9" | bc -l)
   k=$(echo "scale=2;$c+273.15"|bc -l)
   echo -e "\n\e[1;30m=$c c & $k k\e[0m\n"
elif (( $k == "1" ));then
   k=$(echo $1 | sed 's/k//g');
   c=$(echo "scale=1;$k-273.15" | bc -l)
   f=$(echo "scale=1;($c*(9/5))+32" | bc -l)
   echo -e "\n\e[1;30m=$c c & $f f\e[0m\n";
else
   c=$(echo "scale=1;($1-32)*(5/9)" | bc -l)
   f=$(echo "scale=1;($1*(9/5))+32" | bc -l)
   echo -e "\n\e[1;30m=$c c  & $f f\e[0m\n"
fi





-------------------------------------------------------------------------------------------

Script is named temp in my computer.If you want to convert 40 degree centigrade to other scale type temp 40c in terminal and press enter ..





For converting Fahrenheit or kelvin scale type temp 45f and temp 45k respectively. If no scale is specified script will show the given value converted  from centigrade to Fahrenheit and form Fahrenheit to centigrade.


Enjoy Linux !!

Don't know what to do with these codes ?? click here


No comments:

Post a Comment