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 ./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? |
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. |
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
|
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. |
Code:
echo "4 * 0.1" | bc Code:
awk 'BEGIN{print 4 * 0.1}' |
Python, maybe?
BASH is not intended for math..... |
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.
|
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. |
Quote:
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. |