How to calculate float numbers in shell script? -


i'm trying calculate float numbers in shell these commands:

zmin='0.004633'           zmax='3.00642'   step='0.1'   echo "zmin=$zmin"         echo "zmax=$zmax"   echo "step=$step"   n=`echo "(($zmax - $zmin)) / $step " |bc -l `   b=${n/\.*}   echo "b=$b"    ((j = 1; j <= b; j++))         z_$j=`echo  "scale=7; (($zmin + $(($j-1)))) * $step" |bc -l`       zup_$j=`echo  "scale=7; $((z_$j)) + $step " |bc -l `       echo "z_$j=$((z_$j)) && zup_$j=$((zup_$j))" done 

but receive correct answer n. z_$j & zup_$j i'm receiving error:

'z_9=.8004633: command not found'   

how can solve problem?

your problem isn't floating-point, it's can't build variable name this. if using strict posix shell, need use eval this:

tmp=$( echo "scale=7; ( $zmin + $j - 1 ) * step" | bc -l ) eval "z_$j=$tmp" 

however, loop using not posix feature, implies using bash or other shell supports arrays, should use one.

for ((j=1; j<=b; j++))     z[j]=$( echo "scale=7; ( $zmin + $j - 1 ) * $step " | bc -l )     zup[j]=$( echo "scale=7; ${z[j]} + $step" | bc -l )     echo "z[$j]=${z[j]} && zup[$j]=${zup[j]}" done 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -