Sunday, March 4, 2018

bash question

I know my brain is not working as it did before the stroke.  I used to maintain a 6000 line ksh script for HP.  (Yes, I refactored into slightly less monstrous scripts.)

#!/bin/bash
bc <     for (x=0.0; x>-1.0; x -= 0.01) printf "%f\n" x
EOF
The error is (standard in) 1: syntax error

Yes the goal is 0.00 to -1.00 in steps of 0.01.

4 comments:

  1. how about just running "seq 0 -0.01 -1" ?

    this also gets the job done for me on freebsd, if you wanted to get some fancier arithmetic in there, later:

    #!/usr/local/bin/bash
    bc << EOF
    for(x=0.0; x>-1.0; x-=0.01) print x,"\n";
    EOF

    ReplyDelete
  2. echo 'scale= 4; i = 0.00 ; while (i <= 1) { print i, "\n"; i+=.01 }' | bc

    Or

    echo 'scale= 4; for ( i = 0.0; i<=1.0; i+=.01 ) { print i, "\n"}' | bc

    ReplyDelete
  3. Clayton:

    Try this:

    #! /bin/bash
    for x in {0..100}
    do
    printf "%0.2f\n" $(bc -l <<EOF
    0-$x/100
    EOF
    )
    done

    ReplyDelete