1755118 Members
3092 Online
108830 Solutions
New Discussion юеВ

integer math in ksh

 
SOLVED
Go to solution
dictum9
Super Advisor

integer math in ksh

How does one multiple two variables in standard ksh?
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: integer math in ksh

Hi:

# X=2;Y=8;Z=$((X*Y));echo ${Z}
16

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: integer math in ksh

Hi (again):

...I should add that if you are doing arithmetic in a shell, 'typeset'ing your variables to integers speeds things up a bit, too:

#!/usr/bin/ksh
typeset -i X=2
typeset -i Y=8
typeset -i Z
Z=$((X*Y))
echo ${Z}

Regards!

...JRF...
dictum9
Super Advisor

Re: integer math in ksh


The following works on the command line, but doesn't work in the shell script. The exact line that blows up is the multiplication line.



#!/bin/ksh

# find total space available in each VG

for VG in `strings /etc/lvmtab |grep -v dsk`
do
echo processing $VG
TOTAL_PE=`vgdisplay -v $VG | grep ^"Total PE" | awk '{print $3}'`

PE_SIZE=`vgdisplay -v $VG | grep ^"PE Size (Mbytes)" | awk '{print $4}'`

echo TOTAL_PE_=$TOTAL_PE
echo PE_SIZE=$PE_SIZE

((TOTAL_SIZE=$TOTAL_PE * $PE_SIZE)) # does not work in ksh
# ((TOTAL_SIZE=$TOTAL_PE * $PE_SIZE)) # works in dtksh

echo TOTAL_SIZE=$TOTAL_SIZE

done


Oviwan
Honored Contributor

Re: integer math in ksh

Hey

change this:
((TOTAL_SIZE=$TOTAL_PE * $PE_SIZE))
to this:

TOTAL_SIZE=$(( ${TOTAL_PE} * ${PE_SIZE} ))

Regards
James R. Ferguson
Acclaimed Contributor

Re: integer math in ksh

Hi:

Exactly what error do you see? This works for me. You could try running with a trace:

# ksh -x my.sh 2>&1|more

Regards!

...JRF...
dictum9
Super Advisor

Re: integer math in ksh

Oviwan,

this worked beautifully, thanks.

dictum9
Super Advisor

Re: integer math in ksh

James Ferguson,

that one works well also:


total_size=$((total_pe*pe_size))