LinuxQuestions.org
Visit Jeremy's Blog.
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 08-12-2013, 05:59 PM   #1
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Maths equation with output


Hi all!

I have a quick question relating to a simple maths problem and I require the output in a separate file (not displayed on screen). Bash scripting....


$Temp = 45000
I am simply trying to divide $Temp / 1000 and then output the answer to another file $NewTemp

Something like:

$Temp / 1000 >> $NewTemp

I hope that makes sense, as I am really wanting to cat $NewTemp in the script, but I need to divide the temp by 1000 to make the value correct.

Puzzled as the keyboard is not agreeing with me

Cheers
 
Old 08-12-2013, 06:11 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
Bash is not the best solution for any non-integer maths (division). For accurate maths results use bc:
Code:
#!/bin/bash

TEMP=45000

echo $(echo "$TEMP/1000" | bc -l) >> ./file.txt
Please note that you don't put the dollar sign when you declare a variable. $ is only used for retrieving it later.

HTH
 
Old 08-12-2013, 06:42 PM   #3
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks for your reply

I have tried that and the script is spitting out:-
bc: command not found

I am running this on the raspberry pi (debian) and I am puzzled as to why the bc command is not working

My line is:-

Quote:
Temp="/home/local/temp"
CPUTemp="/home/local/cputemp"
cat /sys/class/thermal/thermal_zone0/temp >> $Temp
echo $(echo "$Temp / 1000" | bc -l) >> $CPUTemp
Have tried the bc command with - "lower case L" and - 1 "one"

Spitting out :- bc: command not found

Why could I be missing the bc solver?

Last edited by vk4led; 08-12-2013 at 06:44 PM.
 
Old 08-12-2013, 06:44 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,465

Rep: Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069
bc is a separate program, and it sounds like your pi installation doesn't have it installed by default. If it has a package manager built in you should be able to install it.
 
Old 08-12-2013, 06:48 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
You need to install bc if it's not already installed. On Raspbian you'll probably need to try:
Code:
apt-get install bc
 
1 members found this post helpful.
Old 08-12-2013, 09:51 PM   #6
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks very much for the help.

I have now got it working...

There is always a reason as to why something is not working.... I might be back soon to expand on the question.

Cheers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help with maths percentages clifford227 General 8 03-17-2011 09:22 PM
maths questions sycamorex General 19 06-22-2010 10:56 AM
please help me do maths :) darkRoom General 36 04-11-2005 01:03 PM
problem with maths redduck666 General 7 02-12-2005 02:33 PM
And some maths :-P Mathiasdm General 7 11-11-2004 03:48 PM


All times are GMT -5. The time now is 11:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration