Echo not displaying variable in If-Else condition

 
if [ "${x_oug}" -ne 0 ]
then
echo "Entry Valid                  :  ${x_oug}"
else
echo "Entry Invalid                  : " 0
fi
 

In the above code the 3rd line is not working... it does not print anything

I tried following as well .. but no luck!

echo "Entry Valid                   :   ${x_oug}"
echo "Entry Valid                   :   " ${x_oug}
echo "Entry Valid                   :   $x_oug
echo "Entry Valid                   : " "${x_oug}"
 
 

However, if I put a constant like following it works correctly

echo "Entry valid                  : 5 "

In fact I am forced to use if else as echo not prinitng the variable when it has 0 value

Please help me on this

Thanks and Regards
Chetanz

How are you setting x_oug?

What's the value of x_oug ? How do you assign it? Did you run the script with the xtrace option set?

Thanks for quick replies

I am retriving it from a file which is in fact output of vmstat... eliminating header, awking a particular column values and then getting max of that to set x_oug

x_oug=`cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print x7}'|grep -Ev '(#.*$)|(^$)'|grep -v System|awk '{if($1>mx)mx=$1;}END{print mx}'`

Regards
Chetanz

You know you can make THAT statement way more legible and efficient?

Pls post output of echo "$x_oug"

Hello

Output remains as following i.d. NULL

Entry valid                  :

Regards
Chetanz

I have no idea what you thought you should get out of this, but the awk in the middle of this pipeline ( awk '{print x7}' ) prints an empty line for every line it reads. The two greps that follow that are no-ops, and the awk after that converts one or more empty lines into a single empty line. The ksh test -eq operator is intended to compare two numeric values. Since an empty string is not a numeric value, I'm not sure the results of the comparison are defined, but I expect that ksh will say that [ "" -eq 0 ] evaluates to false rather than giving a syntax error.

Brilliant, Don! I didn't see that x7 , took it for $7 (although suspected an empty variable). Soooo - x_oug being the empty string which is -ne "0" yielding the output as posted!

Hello Friends

Thank you so much for your quick help

I got the issue

In the following line

cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print $8}'|grep -Ev '(#.*$)|(^$)'|grep -v System|awk '{if($1>mx)mx=$1;}END{print mx}'

the logic upto following part is fine

cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print $8}'|grep -Ev '(#.*$)|(^$)'|grep -v System|

till this point we are getting values as

0
0
0

and when logic is applied over it to get MAX as following, to my surprise it is giving us MAX of 3 zeros as NULL

awk '{if($1>mx)mx=$1;}END{print mx}'

Regards
Chetanz

---------- Post updated at 07:30 AM ---------- Previous update was at 07:19 AM ----------

Hello

The results in previous post can be checked as following

#> cat >tst.txt
0
0
0
 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt

Now change values in tst.txt and run following

 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt

Now that the error is found, the problem is not solved .. how to get max of multiple 0 values?

Regards
Chetanz

Try:

printf "%d\n" 0 0 0|awk 'mx==""||$1>mx{mx=$1}END{print mx}'

Clear and efficient is a BEGIN section - the loop section stays small:

awk 'BEGIN {mx=0} $1>mx {mx=$1} END {print mx}'

In this case you can also do a cast to integer by a +0 :

awk '$1>mx {mx=$1} END {print mx+0}'

Probably not an issue for this thread, but note that neither of these will work correctly if all of the values fed into these scripts are negative.
If you try the script I suggested with negative values, such as:

printf "%d\n" -4 -1 -2|awk 'mx==""||$1>mx{mx=$1}END{print mx}'

it still works.

1 Like

Hello Don, Rudic, MadeInGermany

Thank you so much for your help

@Don - I used the snippet mentioned in your post.. it is really charming !

Regards
Chetanz

I was thinking about sth. like initializing max to getconf INT_MIN but there may be negative REAL numbers (-1E20) so this approach wouldn't work either.