LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 07-31-2009, 04:00 PM   #1
KBriggs
Member
 
Registered: Jun 2009
Posts: 35

Rep: Reputation: 15
bash sccripting multiplication


Hey,

I am having trouble doing this multiplication. POssibly it doesn't like the way I am assigning things, I dunno, but maybe someone more experienced can spot the error:
Code:
dE=0.1 
dP=0.1 
...
..

for ((initial_tot_energy_counter=0; initial_total_energy_counter<=100; initial_total_energy_counter++ )); do
for ((impact_parameter_counter=0; impact_parameter_counter<=100;impact_parameter_counter++ )); do

    initial_tot_energy=$(( ${initial_tot_energy_counter}*${dE} ))
    impact_parameter=$(( ${impact_parameter_counter}*${dP} ))

done
done
The problem is that I get the error:

./run_simulations.sh: line 312: 0*0.1 : syntax error: invalid arithmetic operator (error token is ".1 ")

When I try to run it. What am I doing wrong? Does it not like integer * floating points?
 
Old 07-31-2009, 04:13 PM   #2
rtg
Member
 
Registered: Aug 2005
Location: Ukraine
Distribution: Ubuntu 9.04
Posts: 99
Blog Entries: 3

Rep: Reputation: 19
Sorry, bash accepts integers only.

From bash info page:

Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
 
Old 07-31-2009, 04:18 PM   #3
KBriggs
Member
 
Registered: Jun 2009
Posts: 35

Original Poster
Rep: Reputation: 15
There must be something I can do to get the floating point value. What about bc? I don't really know how to use it though
 
Old 07-31-2009, 04:40 PM   #4
rtg
Member
 
Registered: Aug 2005
Location: Ukraine
Distribution: Ubuntu 9.04
Posts: 99
Blog Entries: 3

Rep: Reputation: 19
I suggest you use perl, since i expect that you want to run some external apps as well.
You can use bc mixed with bash, but it will look scary.
 
Old 07-31-2009, 04:43 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
echo "4 * 0.1" | bc
this is a simple example. Basically bc has its own language (http://www.gnu.org/software/bc/manual/html_mono/bc.html), so that you can just write the proper statements and pass them to bc through standard input. The result will be displayed or assigned to a variable. Another way to do calculation is using awk:
Code:
awk 'BEGIN{print 4 * 0.1}'

Last edited by colucix; 07-31-2009 at 04:45 PM.
 
Old 07-31-2009, 05:58 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Python, maybe?

BASH is not intended for math.....
 
Old 07-31-2009, 09:31 PM   #7
KBriggs
Member
 
Registered: Jun 2009
Posts: 35

Original Poster
Rep: Reputation: 15
Other languages aren't really an option, since this is not my script - it is part of a collaboration, so switching languages will be much more trouble than it's worth. I'll figure something out. Thanks all.
 
Old 07-31-2009, 10:14 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Calling bc, awk or whatever command from the shell isn't switching languages, just leveraging them. This is how the shell works by design.

Another option would be to install ksh93 which handle floating points. Your script should work unmodified.
 
Old 08-01-2009, 03:33 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by KBriggs View Post
Other languages aren't really an option
That's true that bc and awk have their own syntax and you can even write a script entirely in bc or in awk, but to perform little operations you can consider them as shell utilities or better just as external commands.
On the other hand it's hard to believe that your script is written in pure bash, without a grep or a cat command! Otherwise, follow the suggestion by jlliagre and install ksh93.

Last edited by colucix; 08-01-2009 at 03:39 AM.
 
  


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 multiplication WITH decimals anonguy9 LinuxQuestions.org Member Success Stories 0 03-27-2009 10:27 PM
Two ADSL Connections Multiplication or Failover AboGassir Linux - Networking 3 07-10-2007 09:10 AM
USB drive, multiplication of mountpoints Jon_Roland Fedora 0 11-07-2005 11:18 PM
floating point multiplication irfanhab Programming 0 12-23-2004 11:13 PM
Parallel matrix multiplication abdobl Programming 3 09-22-2004 07:11 AM


All times are GMT -5. The time now is 06:19 AM.

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