You can do this with bash
's built-in string matching. Note that this uses glob (wildcard) patterns, not regular expressions.
if [[ $(cat /etc/redhat-release | awk '{print $7}') == 7.[56] ]]
Or, of we eliminate the UUoC:
if [[ $(awk '{print $7}' /etc/redhat-release) == 7.[56] ]]
or...
if [[ $(cat /etc/redhat-release) == *" release 7."[56]" "* ]]
or even (thanks to @kojiro)...
if [[ $(< /etc/redhat-release) == *" release 7."[56]" "* ]]
(Note that wildcards at the beginning and end are needed to make it match the entire line. The quoted space after the number is to make sure it doesn't accidentally match "7.50".)
Or if you really want to use regular expressions, use =~
and switch to RE syntax:
if [[ $(< /etc/redhat-release) =~ " release 7."[56]" " ]]
(Note that the part in quotes will be matched literally, so .
doesn't need to be escaped or bracketed (as long as you don't enable bash31
compatibility). And RE matches aren't anchored by default, so you don't need anything at the ends like in the last one.)
VERSION_ID
from the intended-to-be-machine-readable/etc/os-release
is easier than parsing the human-readable string in/etc/redhat-release
. Luckily for doing that, this is RHEL (where bug #1240624 was fixed in version 7.2) and not CentOS or Debian or Arch. https://unix.stackexchange.com/a/382537/5132 https://unix.stackexchange.com/a/125241/5132 – JdeBP Mar 10 '20 at 14:22