Monday, February 17, 2014

KSH Script to Compare First Turn Two Values As Numerics and Then Compare Them

www.unixbabuforum.inI have to write a script that compare two numerics values V1 and V2 etc ... 
For instance : 
if [ X1 -gt X2 ]; then 
echo X1 is greater then X2 etc ... 

My problem is how to turn those two values as numerics , because : 
V1 is the value of the estimate size of an mksysb 
V2 is the available space on file system on where to create the mksysb 
And the purpose is to estimate the size of the mksysb before creating it or not. 

The command to estimate the mksysb file is (it works fine) : 
df -tk `lsvgfs rootvg` | awk '{total+=$3} END {printf "Mksysb size is : %.2f GB\n", total/1024/1024}' 
EXP : 
# df -tk `lsvgfs rootvg` | awk '{total+=$3} END {printf "Mksysb size is : %.2f GB\n", total/1024/1024}' 

Mksysb size is : 6.71 GB 

The command to estimate the available space to put the mksysb , here in /tmp, also works fine : 
df -g '/tmp' | grep -vi free | awk '{espace_dispo=$3} END {printf "Avalable space in /tmp for mksysb is : %.2f GB \n", espace_dispo}' 
EXP : 
# df -g '/tmp' | grep -vi free | awk '{Avail_space_for_mksys=$3} END {printf "Avalable space in /tmp for mksysb is : %.2f GB \n", Avail_space_for_mksys}' 

Avalable space in /tmp for mksysb is : 6.55 GB 

Know I need to put the two values in numerics variale to compare them and start an mksysb or not. 

How to do that. 

www.unixbabuforum.inuse the set command and the $( ... ) subshell. you only need awk to add up the file space requirements. (I used fs available on my system, yours will be different) 

Do something like this: 

~.$ set -- $(df -g /tmp| grep -vi free) 
~.$ set -- $3 $(df -tk devfs | awk '{total += $3}; END { print total}') 
~.$ echo $* 
130 192 

In the first line, you SET the positional parameters $1, $2, $3, ... where $3 is the one you are looking for. 

In the second line, you use $3 to produce $1 (the first position), and the $( df ... ) command produces $2. 

So, the "echo" shows the value of $1 and $2. you might proceed: 

[[ $1 -gt $2 ]] && { echo $1 is greater than $2; do whatever you want with either; } 

or fix the logic to suit your needs.

www.unixbabuforum.inif you want to get fancy, with a simple function, you can do it all on one line: 

~.$ sum3rd () { awk '{total += $3}; END { print total}'; } 
~.$ set -- $(df -g /tmp|sum3rd) $(df -tk devfs | sum3rd) 
~.$ echo $* 
130 192 

Now the first line defines a function, "sum3rd", which sums the 3rd column, since it's the value you want in both cases. 

In this case, the second line returns one value from the "df -g" and the like value from "df -tk", so now the "df -g" value is in $1 and the "df -tk" value is in $2. 

www.unixbabuforum.inuse the "[[ ... ]]" form of test, and "&&" for True, and "||" for False/Else, 
so, in your case: 

[[ $1 -lt $2 ]] && { 

echo "$1 is less than $2" 

# now, enter whatever command you need to do 

you may either reverse the order of comparison: $2 -lt $1 ... 
or change change the test : $1 -ge $2 

look at the "test" manual page for the full set of comparison operators.
www.unixbabuforum.inThere was a bunch of discussion a few months ago as to whether ksh does float operations. I have systems here where some ksh is ksh93t and does do floats, and another ksh is vanilla ksh and does not understand the decimal point. Bash may also vary by system. 

If your shell does not like comparing floats, I would stuff the values into awk like: 

echo $1 $2 | awk '{ print ($1 > $2) ? "BIGGER" : ($1 < $2) ? "SMALLER" : "EQUAL" }'

Then you can test it independently, and then do a string test in the shell.


Post a Comment

Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM