Mike Purcell
Mike Purcell

Reputation: 19989

Linux - Bash - Get $releasever and $basearch values?

I'm writing a bash script to pull packages from remote repos, using reposync, so I can point my nodes to pull locally. As such I am trying to keep the local repo configs as similar as possible to the usptream repo configs, like this:

# upstream
baseurl=http://mirror.freedomvoice.com/centos/$releasever/os/$basearch/

# local
baseurl=http://user:[email protected]/centos/stable/$releasever/os/$basearch/

Within the bash script, is there a cleaner way to get $releasever and $basearch values? I was thinking of doing the following:

yum_metadata=$(yum version nogroups)

Which returns:

Loaded plugins: versionlock Installed: 6/x86_64 360:6167019baac7e76f94c26320424dc41a7f046a70 version

Then regexing for the 6/x86_64 values. Kind of messy, and looking for a more elegant approach.

Upvotes: 19

Views: 36713

Answers (2)

Boop
Boop

Reputation: 1386

To get the value of variables releasever and basearch please see the unix.stackexchange question which has been answered by Mark McKinstry (link to MarkMcKinstry).

Stackoverflow discourages link only answers so here is a short code snippet from the answer:

# enterprise linux major version 8 and up
python3 -c 'import dnf, json
db = dnf.dnf.Base()
print(json.dumps(db.conf.substitutions, indent=2))'

Upvotes: 2

alvits
alvits

Reputation: 6758

Most distro uses the distroverpkg version to get the releasever and basearch.

If you look at /etc/yum.conf, you will see that distrover is set to redhat-release (for RHEL), enterpriselinux-release (for OEL), and others.

To get the package name:

distro=$(sed -n 's/^distroverpkg=//p' /etc/yum.conf)

To get the releasever:

releasever=$(rpm -q --qf "%{version}" -f /etc/$distro)

To get the basearch:

basearch=$(rpm -q --qf "%{arch}" -f /etc/$distro)

The new code above will try to get the package associated with a file /etc/$distro. Some Linux adds /etc/redhat-release to their package release.

If you get file not owned by any package then use the /etc/*-release file that came with your distro. It is probably /etc/centos-release.

You can check the appropriate /etc/*-release appropriate for this code by checking which file is packaged with centos.

rpm -qf /etc/*-release

Then use this file instead of the first line above.

distro=/etc/centos-release

Here's an example from OEL where /etc/redhat-release is packaged as enterprise-release.

rpm -q --qf "%{name}" -f /etc/redhat-release

Output:

enterprise-release

Upvotes: 26

Related Questions