Operating System - Linux
1757033 Members
2294 Online
108858 Solutions
New Discussion юеВ

Re: multiplication in a shell script

 
SOLVED
Go to solution
Jacques Carriere
Regular Advisor

multiplication in a shell script

How can I devide a real number in a shell script.
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: multiplication in a shell script

Hi Jacques:

Most shells only support integer arithmetic.

# X=100;Y=25;((Z=X/Y));echo $Z

If you want to incorporate real numbers, 'bc' provides a convenient vehicle. Choose the 'scale' to set the precision you want:

# X=1;Y=8;echo "scale=3\n $X/$Y"|bc

You can also leverage 'awk' and couple it with 'printf' to format output, including moving to a different number base:

# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'

# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'

If you use the Korn93 shell, its 'typeset' allows a more direct approach to real numbers:

#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo $R1
let R2=1/8
echo $R2
let R3=1/8
echo $R3
exit 0

Regards!

...JRF...
Jacques Carriere
Regular Advisor

Re: multiplication in a shell script

thanks 10 points submitted
Jacques Carriere
Regular Advisor

Re: multiplication in a shell script

this is what I was looking for THX