Operating System - Linux
1752815 Members
5768 Online
108789 Solutions
New Discussion юеВ

Re: Counter function problems...

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Counter function problems...

Ok, here is the issue I have..

I wrote a script that builds disk groups using vxvm, and it works really good. I added a feature that would tell you the max size you could build your your lvol to. I do this by getting the size of the disks I am using in KB, I subtract 5104 from it, and then I multiply it by the number of disks I am using to build my disk group. I set this to a variable called $NUMBER for use throughout the script. The function is finished, it outputs the answer to a file called value.txt. For some reason it works great until $NUMBER = 5 or more.

Below is the section of the script that is giving me issues:
-----------------------------------------------
function adder_func
{
TYPE=$1
if [ -s sizes.$TYPE ]; then
let sum=0
for i in `cat sizes.$TYPE`
do
((i=$i - 5104))
((new_sum=$i + $sum))
sum=$new_sum
done
((last_sum=$new_sum*$NUMBER))
echo ${last_sum}
else
break
fi
}

## Call the adder_func function and output to value.txt to get the max size for your lvol.
adder_func size > value.txt
-----------------------------------------------

When the function is with an option called 'size' it reads from a file called sizes.size (sizes.$TYPE). As you see, the size option is the same as the $TYPE variable.

Output from sizes.size (which is the size of the disks in KB):
524288000

Here is the output from value.txt when the $NUMBER variable = 5:
-1149269920

When I do the math, of course it shouldn't be a large negative number:

root@werver:/root_home> bc -l
524288000-5104
524282896
524282896*5
2621414480

So it should be 2621414480 or 2499.97GB, or about 2.5TB. Am I surpassing the numerical limits of the function?


4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Counter function problems...

Hi Patrick:

Shell arithmetic is 32-bit so, yes, you have exceeded its ability.

You could use 'bc', Perl or simply scale your math by 1024 or 1024*1024 to reach a reasonable approximation.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Counter function problems...

I see. I actually have a second function to do scaling down to GB readable text.
---------------------------------------------
function adder_func2
{
TYPE=$1
if [ -s value.$TYPE ]; then
let sum=0
for i in `cat value.$TYPE`
do
((i=$i/1024))
((new_sum=$i + $sum))
sum=$new_sum
done
((gig_sum=$new_sum/1024))
echo ${gig_sum}G
else
break
fi
}

## Call the adder_func2 function and output to value2.txt to get the
## max size for your lvol in GB.
adder_func2 txt > value2.txt
---------------------------------------------
When the function is ran with an option called 'txt' it reads from a file called value.txt (value.$TYPE). As you see, the txt option is the same as the $TYPE variable.


Here is the output of value2.txt, which just took the value from value.txt (-1149269920) and divided it by 1024 twice:
-1096G

This is why I would do such a thing:
---------------------------------------------
VALUE=`cat value.txt`
VALUE2=`cat value2.txt`
echo "Enter the size of your lvol."
echo "The maximum size is: $VALUE or $VALUE2 "
echo
print -n "Lvol size :"
---------------------------------------------
This is the resulting output:

Enter the size of your lvol.
The maximum size is: -1149269920 or -1096G

I want to give the option of both KB or GB readable for size options, but it looks like it may be better to just scale to GB readable, and be done with it.


Perhaps I should combine the two.
Patrick Ware_1
Super Advisor

Re: Counter function problems...

I fixed it by combining them into one:

---------------------------------------------
function adder_func
{
TYPE=$1
if [ -s sizes.$TYPE ]; then
let sum=0
for i in `cat sizes.$TYPE`
do
((i=$i-5104))
((new_sum=$i + $sum))
sum=$new_sum
done
((meg_sum=$new_sum/1024))
((gig_sum=$meg_sum/1024))
((last_sum=$gig_sum*$NUMBER))
echo ${last_sum}G
else
break
fi
}

## Call the adder_func function and output to value.txt to get the max size for your lvol.
adder_func size > value.txt
---------------------------------------------

And now the output of value.txt:
2495G

And the effect:

Are you ready to create a lvol in fakedg?
Enter 'y' for Yes or 'n' for No :y

Enter the name of your lvol.

Lvol name :fakelvol

Enter the size of your lvol.
The maximum size is: 2495G

Lvol size :

James R. Ferguson
Acclaimed Contributor

Re: Counter function problems...

Hi (again) Patrick:

As an aside:

# VALUE=`cat value.txt`

...is faster if you do (instead):

# VALUE=$(
Regards!

...JRF...