Check Rocky Linux release in shell script

Hi,

This is basically a Bash scripting question, but since it concerns Rocky Linux, I thought I might as well ask here.

The following code snippet checks if the script is running on Rocky Linux 8.x and exits if this is not the case:

# Make sure we're running Rocky Linux 8.x.
if [ -f /etc/os-release ]
then
  source /etc/os-release
  OS="${ROCKY_SUPPORT_PRODUCT}"
  VER="${ROCKY_SUPPORT_PRODUCT_VERSION}"
else
  echo
  echo "Unsupported operating system." >&2
  echo
  exit 1
fi
if [ "${OS}" != "Rocky Linux" ] && [ "${VER}" != "8" ]
then
  echo
  echo "Unsupported operating system." >&2
  echo
  exit 1
fi

Is there a way to write this in a more elegant in compact manner? My Bash scripting skills are a bit rusty.

Thanks & cheers from the sunny South of France,

Niki

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.

1 Like

Do the variables ROCKY_SUPPORT_PRODUCT and ROCKY_SUPPORT_PRODUCT_VERSION exist in os-release?
I bet that they don’t, if system is not Rocky.

What happens in second if when OS=""?

What does man os-release say about variables that always have a value (and are thus safer to use)?

Hi,

Was looking at something for installer script on other project, this should do what you want in less steps:

#!/bin/bash
OS=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
VER=$(awk -F= '/^ROCKY_SUPPORT_PRODUCT_VERSION/{print $2}' /etc/os-release)
if [[ $OS != *"Rocky"* ]] && [[ $VER != "8" ]]; then
  echo
  echo "Unsupported operating system." >&2
  echo
  exit 1
fi

Thanks Tom.

1 Like

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
2 Likes

If you want to make something more generic that will work on pretty much every distribution:

#!/bin/bash
#
# Distro info

ID=`lsb_release -i | awk '{print $3}'`
RELEASE=`lsb_release -r | awk '{print $2}'`
CODENAME=`lsb_release -c | awk '{print $2}'`

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.

2 Likes

Thank you very much for all your numerous suggestions. First post here, and I already love this forum.

Here is what I use. It is a script I found some place and then added to
it over time.

Greg Ennis

#!/bin/bash
echo -n "Running "
RES=uname -a | grep 64
if [ $? -eq 0 ]; then
echo -n "64-bit "
else
echo -n "32-bit "
fi
echo -n "operating system on a "
RES=cat /proc/cpuinfo | grep " lm "
if [ $? -eq 0 ]; then
echo -n "64-bit "
else
echo -n "32-bit "
fi
echo “machine”
RELEASE=cat /etc/redhat-release
OS=uname -r
CPU=cat /proc/cpuinfo | grep -m 1 'model name'
BOG=cat /proc/cpuinfo | grep -m 1 'bogomips'
PRO=cat /proc/cpuinfo | grep 'processor'
echo “$RELEASE with $OS kernel”
echo “$CPU”
echo “$BOG”
echo “$PRO”

I’ve shifted to using ‘Ansible’ for config management. Alas, version in RPM does not parse Rocky properly?

ansible 2.9.23-1 from EPEL should handle Rocky now that it has been patched.