Bash range with negative and float numbers

Click For Summary

Discussion Overview

The discussion revolves around creating a Bash script that utilizes floating-point and negative numbers in a loop to execute a program called new3body. Participants explore issues related to syntax errors, variable assignments, and the use of the bc command for floating-point arithmetic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • The original script encounters a syntax error due to the use of floating-point numbers in arithmetic operations, which Bash does not support directly.
  • Participants discuss the placement of variable assignments within loops, suggesting that the assignment of D should occur before the while loop.
  • One participant points out an 'ambiguous redirect' problem related to output file naming conventions in the script.
  • Another participant suggests changing apostrophes to grave accents in the variable assignment to resolve issues with command substitution.
  • There is mention of using the sh -vx command for debugging the script to identify mistakes more effectively.

Areas of Agreement / Disagreement

Participants express various suggestions and corrections, but there is no consensus on a single solution to the problems presented in the script. Multiple viewpoints and potential fixes are discussed without resolution.

Contextual Notes

The discussion highlights limitations in Bash's handling of floating-point arithmetic and the need for external tools like bc. There are unresolved issues regarding variable scope and command substitution syntax.

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


Hoo-weee. That is indeed a weird one...
 


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


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.
 

Similar threads

Replies
16
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K