MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys,

I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys.

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' `

Harby.

Anything will match the empty string; regular expressions look for a match anywhere, and finding "nothing" anywhere trivially matches. You need to anchor it to make it more picky about what to find. $7 ~ /^$/ checks for an empty string (beginning of string adjacent to end of string) but it's probably more efficient to simply use equivalence comparison against the empty string.

Also, avoid the Useless Use of grep | grep | awk.

MEM=`ps v $PPID | awk 'tolower ($0) ~ /[d]b2/ { if ($7 == "") print 0; else print $7; }

(For additional succinctness, { print ($7 == "" ? 0 : $7) }.)

hi ,

Thanks for your reply. For some reason still doesn't work for me. I took some parts of your command to at least get it to work using :

MEM=`ps v $PPID | grep -i db2| awk '{ if ($7 == "") print 0; else print $7; }`

and with the debug on I get the below:

MEM2=175804
+ + ps v 1790056
+ grep -i db2
+ awk { if ($7 == "") print 0; else print $7; }
MEM=
+ print

so you can see that the variable MEM suppose to be set to 0 but is not. Thanks again and thanks for your sugesstions. It take a while to come with a good coding techniques. :slight_smile:

Does the grep return any match?

Hi ,

Yes the grep is returning the PPID's and then I execute a ps v PPID as you can see below:

MEM=15244
+ + bc
+ echo 1567008 + 15244
MEM2=1582252
+ + ps v 1847476
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16824
+ + bc
+ echo 1582252 + 16824
MEM2=1599076
+ + ps v 1896670
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=11072
+ + bc
+ echo 1599076 + 11072
MEM2=1610148
+ + ps v 1900608
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=13840
+ + bc
+ echo 1610148 + 13840
MEM2=1623988
+ + ps v 1912972
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=15404
+ + bc
+ echo 1623988 + 15404
MEM2=1639392
+ + ps v 1925356
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16752
+ + bc
+ echo 1639392 + 16752
MEM2=1656144
+ + ps v 1974472
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16012
+ + bc
+ echo 1656144 + 16012
MEM2=1672156
+ + ps v 1998882
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=15852
+ + bc
+ echo 1672156 + 15852
MEM2=1688008
+ + bc
+ echo 1688008 / 1024
TOTAL=1648