Bash range with negative and float numbers

In summary: Animastryfe:In summary, the script changed the title of my thread from "New3body: changing A and i" to "Bash range with negative and float numbers."
  • #1
Animastryfe
81
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
  • #2


Hoo-weee. That is indeed a weird one...
 
  • #3


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


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.
 
  • #5
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.
 
  • #6
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'.
 
  • #7
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.
 

1. What is the syntax for declaring a Bash range with negative numbers?

In Bash, a range can be declared using the following syntax: {start..end}. To include negative numbers in the range, the seq command can be used with the syntax seq start end.

2. Can a Bash range include floating point numbers?

No, a Bash range can only include integers. Floating point numbers cannot be used in the {start..end} syntax or with the seq command.

3. How can I use a Bash range to iterate through a list of negative numbers?

A Bash range can be used with the for loop to iterate through a list of negative numbers. For example, for i in {0..-5}; do echo $i; done will print the numbers 0 -1 -2 -3 -4 -5.

4. Can a Bash range start with a negative number?

Yes, a Bash range can start with a negative number. For example, {-5..5} will include all integers from -5 to 5, including both negative and positive numbers.

5. How can I specify a step size for a Bash range with negative numbers?

To specify a step size for a Bash range with negative numbers, the seq command can be used with the syntax seq start increment end. For example, seq -10 2 10 will create a range starting from -10 and incrementing by 2 until 10 is reached.

Similar threads

Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
1K
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
391
Replies
2
Views
1K
  • Quantum Physics
3
Replies
99
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top