It's unclear whether you require all the packages you list to have a major version number 2, or are just verifying that at least one of them does.
Anyway, if we start with the observation that grep foo | awk { print $1 } can usually be refactored into a single Awk script awk '/foo/ { print $1 }' we can already simplify your script; but it sometimes makes sense to refactor basically all of it into Awk. Perhaps like this:
dpkg -l 'libc6*' | awk '$3 !~ /^2\./ { exit 1 }' && echo ok || echo nope
If you are satisfied with just one package meeting the condition, change to something like
dpkg -l 'libc6*' | awk '$3 ~ /^2\./ { exit 0 } END { exit 1 }' && echo ok || echo nope
As always, foo && success || failure is a shorthand for
if foo; then
success
else
failure
fi
where the longhand probably makes sense if your real script needs to do something moderately more complex than just echo a value.
Do note that the output from dpkg -l is not necessarily suitable for scripts. Maybe use dpkg-query -f '${Version}\n' -W 'libc6' instead for robustness.
dpkg -l | grep libc6output?