Hi
Really sorry to bother but if anyone can help that would be amazing. I am trying to learn bash programming and functions, and so I have written a simple celcius/farenheit converter. But it doesn't work for some reason - even though all the echo to bc commmands work just fine in the terminal with the same syntax.
Code:
#!/bin/bash
VAR1=9
VAR2=5
VAR3=32
cel2far ()
{
read -p "enter number in C followed by <ENTER> ==> " resp
part1=echo "scale=2;$VAR1 / $VAR2" | bc
part2=echo "scale=2;$part1 * $resp" | bc
answer_c2f=echo "scale2;$part2 + $VAR3" | bc
echo "$answer_c2f"
}
far2cel ()
{
read -p "enter number in F followed by <ENTER> ==> " resp
part1=echo "scale=5;$resp - $VAR3" | bc
part2=echo "scale=5;$VAR2 * $VAR1" | bc
answer_f2c=echo "scale=2;$part2 * $part1" | bc
echo "$answer_f2c"
}
echo "Is your temperature in celsius or farenheit [C/F]?"
read temp_type
if [ $temp_type == C ]; then
cel2far
elif [ $temp_type == F ]; then
far2cel
else echo "unrecognized temp format; please type either C or F [caps]"
fi
Bookmarks