stars83clouds
stars83clouds

Reputation: 836

Bash script using negative to cycle through

I'm trying to run a bash script that includes a nested for loop within which a variable should cycle through negative exponents, viz:

for ABPOW in {-11..-9}
do
ABC = $((10**$ABPOW))
          for ABCOEFF in {1..9}
          do
              sed -e 's/ACOEFF/'$ABC'/'\

This is only the inner two for loops of the code. When the values in the first bracket (for ABPOW) are positive, the code runs fine. However, when I have them as i do above, which is what I need, the error communicated to screen is:

./scripting_test2.bash: line 30: 10**-11: exponent less than 0 (error token is "1")

How do I make this run? Thanks in advance.

PS: I tried putting a negative sign in front of $ABPOW but the exponents are still recorded as positive.

Upvotes: 0

Views: 980

Answers (4)

anubhava
anubhava

Reputation: 785491

One alternative is to use awk for your complete script that has full support for floating point arithmetic:

awk 'BEGIN{for(i=-11; i<=-9; i++) {ABC=10^i; print ABC}}'
1e-11
1e-10
1e-09

Upvotes: 1

Brian Cain
Brian Cain

Reputation: 14619

Bash doesn't support floating point data types.

$ man bash|grep -c -i float
0

Do you have python installed?

ABC=$( python -c "print 10**$ABPOW" )

Upvotes: 1

Jim Scarborough
Jim Scarborough

Reputation: 123

Here's another way:

perl -e 'for $abpow (-11..-9) { $abc=10**$abpow; for (1..9) { system("echo $abc"); } }'

Upvotes: 1

user000001
user000001

Reputation: 33367

Bash does not support floating point arithmetic (which is necessary for raising something into a negative power). Instead, you should use the bc utility.

ABC=$(bc -l <<< "10 ^($ABPOW)")

Also, there should be no spaces before and after the = in variable assignments

Upvotes: 1

Related Questions