LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash sccripting multiplication (https://www.linuxquestions.org/questions/linux-newbie-8/bash-sccripting-multiplication-744225/)

KBriggs 07-31-2009 03:00 PM

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?

rtg 07-31-2009 03:13 PM

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.

KBriggs 07-31-2009 03:18 PM

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

rtg 07-31-2009 03:40 PM

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.

colucix 07-31-2009 03:43 PM

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}'

pixellany 07-31-2009 04:58 PM

Python, maybe?

BASH is not intended for math.....

KBriggs 07-31-2009 08:31 PM

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.

jlliagre 07-31-2009 09:14 PM

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.

colucix 08-01-2009 02:33 AM

Quote:

Originally Posted by KBriggs (Post 3627230)
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.


All times are GMT -5. The time now is 08:24 AM.