2

Here is my script:

#!/bin/bash
        for ((i=0.5; i<=2.0; i+= 0.05)); do
            (   ./myExe -l t2000.bin i
            ) &
        done
        wait

and the error is:

((: i=0.5: syntax error: invalid arithmetic operator (error token is ".5")

I know this doesn't work but I've heard that there's a way to do something like it, any suggestions?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 3
    bash only support integers. – Etan Reisner Oct 24 '14 at 18:18
  • I thought about that @EtanReisner, but I heard that it could be done and that's why I asked, but now I got a -1! :/ Moreover, I can't delete the question, because it has answers. – gsamaras Oct 24 '14 at 18:52
  • Yeah, I'm not sure what the `-1` is about there but you might have put that "I know this doesn't work but I've been told there's a way to do something like it, suggestions?" might have gone over better than a question that looked like you just hadn't known that bash doesn't do that. – Etan Reisner Oct 24 '14 at 19:06
  • I see, what you suggest me to do @EtanReisner? Thanks for the tip, I will keep that in mind for the future. :) – gsamaras Oct 24 '14 at 19:07
  • 1
    Updating your question with that extra explanation wouldn't hurt. Beyond that I'm not sure there's anything you can do. – Etan Reisner Oct 24 '14 at 19:10

3 Answers3

7

You can only (easily) loop over integers; use bc to reduce to the correct floating-point value when calling myExe.

for ((i=50; i <= 200; i+=5)); do
    ./myExe -l t2000.bin $(bc <<< "scale=2; $i/100") &
done
wait

Note that bash doesn't work with floating-point values at all. Here, the output of the bc command is just a string, which bash doesn't process, but just passes on to the command as an argument.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • i would go for while loop & add .5 to the seed value inside loop & check for while condition for integer 2 – Ashish Oct 24 '14 at 18:26
  • +2 for both of you beating me by 35 :) – David C. Rankin Oct 24 '14 at 18:26
  • @Ashish `bash` cannot add .5 to a value, nor compare a floating point value to an integer. *Anything* involving floating-point arithmetic must be handled by an external command. – chepner Oct 24 '14 at 18:27
  • I remember at start I used `zcalc` for float calcualtion & `if [ $(echo "$n1 > $n2" | bc) -ne 0 ]` for comparison – Ashish Oct 24 '14 at 18:33
  • `zcalc` is part of a different shell, `zsh`, which *can* handle floating-point arithmetic. – chepner Oct 24 '14 at 18:35
  • yep! agreed.. I just tried an example with $(php -e "echo $i+0.5;") lot many ways to do. thanks – Ashish Oct 24 '14 at 18:44
  • @chepner this did the trick. However, I got a -1 on the question, but I can't delete it, because it has answers. (I don't think that the solution was too obvious, I just thought that integers are only supported by bash, but there must be a workaround...) :/ – gsamaras Oct 24 '14 at 18:57
  • Oh wait, I got `2831 Floating point exception./myexe -l t2000.bin $(bc <<< "scale=2; $i/100")` – gsamaras Oct 24 '14 at 18:59
  • That's a problem with `myexe`, not the shell. – chepner Oct 24 '14 at 19:03
  • I just thought of that too, but if I pass the values by myself in the cmd, it will run as expected. Working on it. – gsamaras Oct 24 '14 at 19:04
  • If you put the `wait` inside the for loop, you may as well just run `myexe` in the foreground. Whatever `myexe` is, it sounds like there are issues with running multiple instances of it in parallel. – chepner Oct 24 '14 at 19:47
4

Without using bc:

for ((i = 50; i <= 200; i+=5)); do
  f=$((i/100)).$((i%100))
  echo $f
done
ooga
  • 15,423
  • 2
  • 20
  • 21
3
for i in 0.{5..9}{,5} 1.{0..9}{,5} 2.0; do echo $i; done

or:

for i in $(seq 0.5 0.05 2); do echo $i; done
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • The other answers are obvious why they work. What exactly happens in the range you provided? – gsamaras Oct 24 '14 at 19:13
  • As Etan Reisner mentioned bash only support integers but bash can do other things like "Brace Expansion". To see what happens paste this to your bash: `echo 0.{5..9}{,5} 1.{0..9}{,5} 2.0`. A simple example: `echo {4..5}{0..3}`. Result: `40 41 42 43 50 51 52 53`. You can abuse "Brace Expansion" with an array also to convert small decimal numbers to binary numbers: http://stackoverflow.com/a/25943519/3776858 – Cyrus Oct 24 '14 at 19:25
  • As much as I love bc, this is a perfect job for seq. – PM 2Ring Oct 25 '14 at 10:26