1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Bash range with negative and float numbers

  1. Jul 7, 2010 #1
    Edit: Why did my thread title change?

    Code (Text):
        #!/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 (Text):
        ./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 (Text):
    #!/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 (Text):
    ./bashangle: line 7: [: too many arguments
     
    Last edited: Jul 7, 2010
  2. jcsd
  3. Jul 7, 2010 #2

    berkeman

    User Avatar

    Staff: Mentor

    Re: * Edit post * Delete post * Report this post * Reply with quote Ba

    Hoo-weee. That is indeed a weird one...
     
  4. Jul 7, 2010 #3

    berkeman

    User Avatar

    Staff: Mentor

    Re: * Edit post * Delete post * Report this post * Reply with quote Ba

    I can change it back for you. What was it called?
     
  5. Jul 7, 2010 #4
    Re: * Edit post * Delete post * Report this post * Reply with quote Ba

    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.
     
  6. Jul 7, 2010 #5

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  7. Jul 8, 2010 #6
    Yes, you're right. However, there's now an 'ambiguous redirect' problem.

    Code (Text):
    #!/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'.
     
  8. Jul 8, 2010 #7

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook