Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
And probably other ways. Bash can handle non-floating point math.
Code:
#!/bin/bash
CELCIUS=20
FAHRENHEIT=68
# F to C
TEMP1=$(echo $(( $FAHRENHEIT - 32 )))
TEMP2=$(echo $(( $TEMP1 * 5 )))
echo "C =" $(( $TEMP2 / 9 ))
# C to F
TEMP1=$(echo $(( $CELCIUS * 9 )))
TEMP2=$(echo $(( $TEMP1 / 5 )))
echo "F =" $(( $TEMP2 + 32 ))
exit 0
You can fake floating point by adding a few zeros to the starting number. And manually adding the decimal when outputted. Otherwise you need "bc" to do floating point in various scripting languages like bash. Which is NOT installed by default on many minimum / headless installs.
If you've got a Centos/RHEL v6 system, it should be in /usr/bin/bc - you may need to check your $PATH.
Its in the base repo, so its unusual for it not to be installed.
The error that I'm receiving is
./celsius_to_fahrenheit.sh line 13: bc: command not found
I'm not sure how to fix this. Please help
done
Hi,
You need to install bc.
But I would like to add: why do you do these kinds of operations in Bash?
Bash is not exactly intended for this kind of programming. If low level languages like C is inaccessible to you, then try Python. It's faily easy and you will be able to do so much more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.