Simple shell script function

Hello, Trying to look

vgcheck() {

   lsvg -o|
             while read vg; do
                        lsvg -p $vg|grep Missing
                       if [ $? -ne "0" ];then
                          echo "OK:PASSED"
                       else
                          echo "FAIL"
                       fi

}

function looks at every VG(volume group on servers having multiple) and if there are disks in missing state says OK. if it doesnt gives error.

Here is output I get :

OK:PASSED
OK:PASSED

I would like the script to give single output of " OK:PASSED " instead of each time. how do i do it?

Iterate through and gives one OK:PASSED if there is no missing in each of the VG.

if [ $? -ne 0 ]; then
    :
else
    echo "FAIL"
    return
fi
echo "OK:PASSED"

Only I don't understand a little why not 0 is not an error? And there is no operator "done"

--- Post updated at 10:08 ---

vgcheck() {
        lsvg -o | 
                while read vg; do
                        lsvg -p $vg | grep Missing
                        if [ $? -ne 0 ]; then
                                :   
                        else
                                echo "FAIL"
                                return #if you need exit
                        fi  
                done
        echo "OK:PASSED"
}

You might want to return an exit code other that 0.

How about a slightly different approach, like (untested; verify the grep options)

vgcheck() {  lsvg -o | while read vg; do lsvg -p $vg; done  | grep -qm1 "Missing"  && echo "Fail" || echo "OK:PASSED"; }

Add an exit code to taste...

Not quite: lsvg -o in AIX (i suppose this is AIX, yes?) gives a list of volume groups in varyon state, not "every volume group". If there is a volume group but you did a varyoffvg <vg> then it will not be included in this list, although it will be displayed in lsvg .

Also notice that in HACMP-clusters nowadays (HACMP 7.1.x and 7.2) VGs are in a "somewhat varyon" state on the passive node(s) if you get your disks from a SAN and the LUNs are in the "no_reserve" state. I am not sure if a lsvg -p would succeed on such a VG even though the disks are there.

Wouldn't it make a lot of sense to include the name of the VG with problems if there is one? You also should not use "echo" in a ksh (you do use ksh, no?) but print , which is a shell-builtin. If you want to be 101% POSIX-compliant use neither but printf .

Anyway, consider this, change the error message to your liking and/or remove the -u2 if you want to redirect the error message to stdout instead of stderr:

vgcheck ()
{
typeset chVG=""

lsvg -o | while read chVG ; do
     if ! lsvg -p "$chVG" | grep -qi "missing" ; then
          print -u2 "Error: ($chVG) disks missing"
          return 1
     fi
done
print "OK:PASSED"
return 0
}

I hope this helps.

bakunin