Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm under Ubuntu 11.10, when I execute this program in bash I have the following error:

exec.bash: line 25: [: 0.1: integer expected as an expression

line 25 is:

while [ $p -le 1.0 ]

in the following context

p=0.1

while [ $p -le 1.0 ]
do
    echo ----probability---- $p
    java $jvmargs -classpath $CP jbenchmarker.MainSimulation $p
    (( p= $p + 0.1 ))
done

where is the problem?

share|improve this question
add comment

2 Answers 2

Your while loop can be replaced by a for-loop. Since Bash cannot handle floating point arithmetic, you should use normal integers instead and use a program like bc for creating floats. The scale=1; command before $i/10 ensures that there is always a single decimal (e.g. 0.1).

for ((i=1; i<=10; i++))
do
    p=$(bc <<<"scale=1;$i/10")
    echo ----probability---- $p
    java $jvmargs -classpath $CP jbenchmarker.MainSimulation $p
done

This is equivalent to:

i=1
while ((i<=10))
do
    p=$(bc <<<"scale=1;$i/10")
    echo ----probability---- $p
    java $jvmargs -classpath $CP jbenchmarker.MainSimulation $p
    ((i++))
done

command <<<... is a neat trick that writes ... to the standard input of command and is equivalent to echo ... | command

share|improve this answer
    
Thanks for all it works. @Lekensteyn : Thanks a lot :) –  user1149157 Mar 19 '12 at 10:07
add comment

Bash does not do floating point arithmetics; only integer arithmetics. To compare or calculate with floats, you have to involve external commands, such as bc or awk. See How can I calculate with floating point numbers instead of just integers?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.