It’s possible to compact that, but honestly unless you /really/ need to shave code for space or optimization then I prefer easily readable code. And this is very readable and clear code. It also passes shellcheck.net which is a great source for verifying Bash code.
Looks good to me.
I would recommend using the ID and VERSION_ID fields. And you can use them directly
Something like:
# Default values in case the file isn't defined or doesn't have the data
ID=
VERSION_ID=
# Load the file if it exists
[[ -f /etc/os-release ]] && . /etc/os-release
# Must be rocky 8
if [[ "$ID" != "rocky" || ! "$VERSION_ID" =~ ^8 ]]
then
echo Only supported on Rocky 8 versions
fi
since the content of /etc/os-release can vary between distros, in particular the keys. I use this between Debian/Ubuntu/Rocky and provides reliable info. Some keys in /etc/os-release are similar between the distros, but some do vary.