awk correction

if awk 'BEGIN{if('$RSS'>='1000')exit 0;exit 1}' ; then
     dowhatever
fi

the above code works great when i'm comparing numerical figures.

but when i try to do a string comparison, it does not work:

if awk 'BEGIN{ /'$RSS'/ ~ /DB01/ exit 0;exit 1}' ; then
    dowhatever
fi

can this code be fixed?

Hello SkySmart,

One simple way is we can avoid the use of awk .

if [["$RSS" == "DB01" ]] #### Considering DB01 is a text, if it is a variable use "$DB01".
then
    dowhatever
fi

EDIT: Also with an awk approach.

echo $RSS
singh
 
if awk -vvar="$RSS" 'BEGIN{ if(var ~ /singh/) {exit 0} else {exit 1} }' ; then
echo "correct"
else
echo "Incorrect."
fi

Output will be as follows.

correct

Thanks,
R. Singh

1 Like

Regarding the awk proposal: that would yield an exit code of 0 also if RSS were a long string containing DB01 somewhere. To make it look for DB01 exactly, use sth like

RSS=DB01
awk -vvar="$RSS" 'BEGIN{ exit 1 - (var == "DB01")  }'
1 Like