Bash range with negative and float numbers

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 6K views
Animastryfe
Messages
80
Reaction score
0
Edit: Why did my thread title change?

Code:
    #!/bin/bash
    A=0
    B=0
    while [ $B -lt 90 ]
    do
    for (( COUNTER=-4.5; COUNTER<=6; COUNTER+=0.1 ));
            do
                    ./new3body $A $COUNTER 2 > outangle$A-$COUNTER 2>&1
            done
    B=`echo "$B+1." |bc -l`
    A=`echo "$A+0.1" |bc -l`
    done

I have never used bash before. I'm trying to make a script that will execute the program new3body while changing 'A' and 'i'. I want the script to run the program for A=0 to A=8.9 while COUNTER=-4.5, then do the same thing again for COUNTER=-4.4, all the way to COUNTER=6. When I tried to run this script, the following error popped up:
Code:
    ./bashangle: line 6: ((: COUNTER=-4.5: syntax error: invalid arithmetic operator (error token is ".5")
I have heard that bash doesn't like to work with non-integers, and I used 'bc' at the end of the program from a google search earlier, but I don't understand how it works. Also, the variable "B" is there solely to act as a counter for "A", because '-lt' doesn't work with floating point numbers.

I also tried this variant, using the same work-around as I used for 'A'. 'COUNTER' is now 'D'.

Code:
#!/bin/bash
A=0
B=0
while [ $B -lt 90 ]
do
C=0
while [ $C -lt 105 ]
D=0
        do
                ./new3body $A $D 2 > outangle$A-$D 2>&1
                C='echo "$C+1." |bc -l'
                D='echo "$D+0.1" |bc -l'
        done
B=`echo "$B+1." |bc -l`
A=`echo "$A+0.1" |bc -l`
done

The error this time is
Code:
./bashangle: line 7: [: too many arguments
 
Last edited:
Physics news on Phys.org


berkeman said:
I can change it back for you. What was it called?

It was originally called "Bash range with negative and float numbers", but since I'm new to bash and programming in general I'm not sure if that is a good title. You change it to whatever title you think reflects the question best.
Thank you.
 
Animastryfe: This is just a guess, but in your second program, shouldn't "D=0" appear before "while," instead of after while? Isn't D=0 in the wrong location? See if that helps.
 
nvn said:
Animastryfe: This is just a guess, but in your second program, shouldn't "D=0" appear before "while," instead of after while? Isn't D=0 in the wrong location? See if that helps.

Yes, you're right. However, there's now an 'ambiguous redirect' problem.

Code:
#!/bin/bash
A=0
B=0
while [ $B -lt 90 ]
do
C=0
D=0
while [ "$C" "-lt" "105" ]
        do
                ./new3body $A $D 2 > out$A$D 2>&1
                C=$(($C+1))
                D='echo "$D+0.1" |bc -l'
        done
B=`echo "$B+1." |bc -l`
A=`echo "$A+0.1" |bc -l`
done

Apparently, something is wrong with 'out$A$D'.
 
Animastryfe: In line 12 of your third program (post 6), change each apostrophe (') to a grave accent (`). (Line 12 is the D assignment statement.) See if this resolves the problem. If not, perhaps try, at the command prompt, sh -vx bashangle, and see if it helps you track down a mistake.