1833151 Members
3613 Online
110051 Solutions
New Discussion

using for loop

 
SOLVED
Go to solution
fahad_8
Advisor

using for loop

how to get the size for more than one disk in one script ..?
what i did is
1- ioscan -funCdisk >>f1
2- cat f1|grep rdsk|awk{'print$2'}>f2
3- for i in 'cat f2'
do
diskinfo $i>>f3
done
4- cat f3|grep size>>f4
5-cat size.out|awk{'print $2'}>f5
6- f5 output , it contains only numbers , i want to use script to calculate the sum of these number ??

waiting for reply
thanx
5 REPLIES 5
Jakes Louw
Trusted Contributor
Solution

Re: using for loop

Try this :

TOTAL=0
for i in `ioscan -funCdisk | grep rdsk | awk ' { print $2 } '`
do
NUMBER=`diskinfo $i | grep size | awk ' { print $2 } '`
# that gives you the size of one disk, now add it to the running total
eval TOTAL=($TOTAL+$NUMBER)

done

echo $TOTAL

Just check my syntax on the eval statement, I always get that wrong.
Trying is the first step to failure - Homer Simpson
john korterman
Honored Contributor

Re: using for loop

Hi,

to sum up:
# awk '{s += $1}END {print s}' < f5

regards,
John K.
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: using for loop

Hi:

Beware that a simple summation of the values returned by 'diskinfo' by walking the output of 'ioscan', is going to count alternate links (pvlinks) more than once, inflating the amount of disk you think you have.

Also, if you fail to skip unmounted CD|DVDROM devices, the 'diskinfo' query will hang.

Regards!

...JRF...

Jeffrey L. Cooke
Occasional Advisor

Re: using for loop

Although ioscan has a limited amount of output, in situations where I don't know the quantity of output I use 'while'. First redirect the output into a file (as you did) and then read the input from stream 3 (an unused stream) as in this example (working for the other folks answers):

#!/usr/bin/ksh
TOTAL=0
ioscan -funCdisk 2/dev/null |\
grep rdsk|\
awk '{print $NF}'\
>ioscan.txt
while read -u3 i; do
SIZE=$(diskinfo $i|\
grep size|\
awk '{ print $2}'\
)
(( TOTAL = TOTAL + SIZE ))
done 3print -- "$TOTAL"
rm -f ioscan.txt
MarkSyder
Honored Contributor

Re: using for loop

for i in `ls /dev/rdsk`
do
diskinfo $i|grep -E "describe|size" >> outputfile
done

will do the first part for you.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing