1

I have an open source shell/bash script for setting up and configuring hosting servers, and to install cloudflare I am pulling the version of RHEL using this command:

RHEL_VERSION=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release) );

The problem is that if there is a minor version, that will be returned instead of just the major version. With the cmd above it will return 7.2 ... problem is, I only need the major version number.

I'm thinking that I can probably just use the rpm -q --whatprovides redhat-release which would return something like cloudlinux-release-7.2-1.el7.x86_64, and then just sed, awk, etc to get just the el7 portion of the string.

I need this to be able to use the string when creating the package URL:

PACKAGE_URL="http://pkg.cloudflare.com/cloudflare-release-latest.el${RHEL_VERSION}.rpm"
1

To snip the major version component, simply do:

... | grep -Eo '^[0-9]*'

The -o option to this extended grep prints only the matched part; here, the first digit(s) preceded by a dot character.

  • Thanks, this solution will work the best, due to the way I am getting the version number, as I found some servers that may be running different versions of CloudLinux, etc, have a different strings or formats, and as such, this works best. Thanks! – sMyles Dec 21 '15 at 1:12
1
$ RHEL_VERSION=7.2-1.el6
$ echo "${RHEL_VERSION%%[!0-9]*}"
7
$ echo "${RHEL_VERSION%%-*}"
7.2
$ echo "${RHEL_VERSION##*.}"
el6
$ echo "${RHEL_VERSION##*[!0-9]}"
6
  • Thanks, this solution works just as good as JRFerguson and had he posted after yours I would have selected this one. Thank you though! – sMyles Dec 21 '15 at 1:13
0

I don't have rpm at hand on this system, but I managed to do this with bash:

PROVIDED=cloudlinux-release-7.2-1.el7.x86_64
RHEL_VERSION=`echo $PROVIDED  | sed 's/^.*-release-\([0-9]\).*/\1/'  
echo $RHEL_VERSION 

which outputs "7" .

If needed, you may replace "-release-" with a more suitable regexp.

You may try things like:

echo $PROVIDED  |  sed -ne 's/^[^-]*-\([^-]*\)-.*$/\1/p'

which returns "release" for me [--version: sed (GNU sed) 4.2.2].

  • Thank you for your response, after testing on multiple servers it looks like some of them return different strings (not including el, or having different format, etc), but I do appreciate your help! Thanks! – sMyles Dec 21 '15 at 1:15
0

You may also want to consult this fantastic answer about determining your OS name (and version) reliably. Based on that answer, you can do something like:

RHEL_VERSION="$(lsb_release -r | awk '{print $NF}')"

and then use any of the other answers provided for extracting the major version number. The advantage is that this approach works for more distros than RHEL.

If you're sure the release number is going to be purely numeric (with a decimal point separating the major and minor numbers, you can do it all in one fell swoop:

RHEL_MAJOR_VERSION="$(lsb_release -r|awk '{printf "%d\n",$N}')"
  • Thanks, I didn't use lsb_release though due to a couple issues with CentOS and Fedora requiring the redhat-lsb package, which sometimes may not be installed. So get around that I used the code above which has worked well so far (haven't had any issues opened about it not), thanks though! – sMyles Dec 21 '15 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.