sidney
sidney

Reputation: 2714

Dividing per a lower number

A shell script is giving me a trouble. It should display some increasing values each iteration. Dividing "1/9" is the source of the problem, and setting count as "1.0" should do the trick but gives me an error instead: 'Illegal number: 1.0'

count=1
rtime=9

until [ $count -eq $rtime ]
do
  echo $((($count/$rtime)*10))
  sleep 1
  count=$(($count+1))
done

Upvotes: 0

Views: 64

Answers (2)

jseanj
jseanj

Reputation: 1559

set -o nounset                              # Treat unset variables as an error
count=1
rtime=9

until [ $count -eq $rtime ]
do
    echo $(echo "scale=2; $((count*10))/$rtime" | bc)                                                                                 
    sleep 1
    count=$((count+1))
done

Upvotes: 1

choroba
choroba

Reputation: 241928

The shell can only do integer arithmetics. For floats, you can try bc.

Upvotes: 0

Related Questions