LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-13-2016, 10:43 PM   #1
JurassicPark
LQ Newbie
 
Registered: Sep 2016
Posts: 1

Rep: Reputation: Disabled
Help on bash converting c to f degrees


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


#!/bin/bash

#Variable to hold user option, initially it is 1
option=1

#Loop till option is 0
until [ $option -eq 0 ]

do
#Prompting user to enter temperature in centigrades
echo -n "\n Input Centigrade temperature value: "

#Reading temperature in centigrades
read centigrade

#Converting temperature from Centigrade to Fahrenheit
fahrenheit=$(echo "scale=2;((9/5) * $centigrade) + 32" |bc)

#Displaying temperature
echo "\n Temperature in Fahrenheit: $fahrenheit "

#Prompting user option
echo -n "\n Press (1 - Continue 0 - Exit): "

#Reading user option
read option
done
 
Old 09-13-2016, 11:12 PM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
# apt-get install bc

# pacman -S bc

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.

Last edited by Shadow_7; 09-13-2016 at 11:17 PM.
 
Old 09-13-2016, 11:36 PM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
The fake floating point version:

Code:
#!/bin/bash
CELCIUS=21
FAHRENHEIT=70
OFFSET=32

# as many zeroes as you want decimal places, within limits ofc
FAKINGIT=1000000

C1=$(( $CELCIUS * $FAKINGIT ))
F1=$(( $FAHRENHEIT * $FAKINGIT ))
O1=$(( $OFFSET * $FAKINGIT ))

# F to C
TEMP1=$(echo $(( $F1 - $O1 )))
TEMP2=$(echo $(( $TEMP1 * 5 )))
TEMP3=$(echo $(( $TEMP2 / 9 )))
echo "F="$FAHRENHEIT "C =" $(( $TEMP3 / $FAKINGIT ))"."$(( $TEMP3 % $FAKINGIT ))

# C to F
TEMP1=$(echo $(( $C1 * 9 )))
TEMP2=$(echo $(( $TEMP1 / 5 )))
TEMP3=$(echo $(( $TEMP2 + $O1 )))
echo "C="$CELCIUS "F =" $(( $TEMP3 / $FAKINGIT ))"."$(( $TEMP3 % $FAKINGIT ))

exit 0

Last edited by Shadow_7; 09-13-2016 at 11:39 PM.
 
1 members found this post helpful.
Old 09-13-2016, 11:53 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I understand that some times error messages can be a bit obtuse or even misleading, but this particular message could not have been any clearer.
 
Old 09-15-2016, 03:39 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 09-15-2016, 03:58 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
The echo is redundant eg:
Code:
keithhedger@severnet ~ $ TEMP1=$(echo $(( 100 - 50 )))
keithhedger@severnet ~ $ echo $TEMP1
50
keithhedger@severnet ~ $ TEMP1=$(( 100 - 50 ))
keithhedger@severnet ~ $ echo $TEMP1
50
 
1 members found this post helpful.
Old 09-15-2016, 05:17 AM   #7
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by JurassicPark View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
LXer: Current Temperature in Hades, 31 degrees F...0 degrees C LXer Syndicated Linux News 0 11-22-2012 11:00 AM
[bash] Current time to degrees for clock wallpaper Raymii Programming 2 11-10-2009 06:01 PM
converting a py script to bash saawan Linux - Software 2 05-06-2009 11:56 AM
converting bash to perl sporty Programming 2 07-14-2006 04:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration