LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-15-2010, 10:42 PM   #1
bluesmodular
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
How to invoke the calculator "bc" in bash script for floating point arithmetic?


Hello,

My main problem right now is doing floating point arithmetic within a bash script, with variables.

Right now I have a folder called "myExamples" with a script called "run_example" that runs with no issues.

I plan to
(1) create many folders inside [myExamples], that are named [example10] [example11]...
each containing an identical copy of (run_example),
(2) modify Line 172 of each copy of (run_example)...
in one copy, it would be 3.00, the next copy would have 3.05, etc. (This part doesn't work!)

How can I use the available calculator bc code to do floating point operations?
Thank you for your help!


My code is below -

#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname

# Make many folders containing executables of slightly different parameters...
for i in `seq 10 11`;
do
mkdir examples$i
done

# Copy the same file into all the folders where (run_example) and [examples{num}] are in ~/myExamples
for i in `ls -d */`;
do cp run_example "$i";
done;

# Replace... something... in (run_example) for each folder
bulksize=`expr 6.00`
half=$(( bulksize / 2)) # THIS LINE DOES NOT WORK

for i in `ls -d */`;
do
cat $i/run_example | sed -e '172 s/[0-9]*/\$half/' > $i/run_example # THIS LINE DOES NOT WORK
half=`expr $half + 0.05`; # THIS LINE DOES NOT WORK
done;



When I replaced
half=$(( bulksize / 2))
with
half=$bulksize/2 | bc

I got
expr: non-numeric argument
 
Old 05-15-2010, 11:38 PM   #2
bluesmodular
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
[SOLVED] ... invoke the calculator "bc" in bash script for floating point arithmetic?

Actually I just solved it -
half=$(echo "scale=2; $bulksize / 2" | bc)
half=$(echo "scale=2; $half + 0.05" | bc)
 
Old 05-16-2010, 12:58 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Please mark as SOLVED using the thread tools so everyone else knows too
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
Floating-Point Arithmetic Not Associative or Distributive? JMJ_coder General 6 09-09-2008 07:24 PM
Yum Upgrade from FC5 "Floating point exception" TheJosh Fedora 0 01-05-2007 11:03 PM
KDE "floating point exception" JRR883 Linux - Desktop 2 12-18-2006 04:45 PM
how can I invoke "awk" from shell to do floating point math? Joseph Schiller Programming 8 01-12-2006 06:00 AM


All times are GMT -5. The time now is 04:58 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration