Linux-Bash-获取$ releasever和$ basearch值? [英] Linux - Bash - Get $releasever and $basearch values?

查看:668
本文介绍了Linux-Bash-获取$ releasever和$ basearch值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,使用reposync从远程存储库中提取软件包,因此我可以将节点指向本地.因此,我正在尝试使本地存储库配置与usptream存储库配置尽可能相似,如下所示:

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:password@repo.example.com/centos/stable/$releasever/os/$basearch/

在bash脚本中,是否有更干净的方法来获取$ releasever和$ 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)

哪个返回:

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

然后对6/x86_64值进行正则表达式.有点凌乱,正在寻找一种更优雅的方法.

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

推荐答案

大多数发行版都使用distroverpkg版本来获取releasever和basearch.

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

如果查看/etc/yum.conf,您将看到该发行版设置为redhat-release(对于RHEL),enterpriselinux-release(对于OEL)以及其他.

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.

要获取程序包名称:

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

要获取发布版本:

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

要获取基拱:

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

上面的新代码将尝试获取与文件/etc/$distro关联的软件包.某些Linux将/etc/redhat-release添加到其软件包发行版中.

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.

如果得到file not owned by any package,请使用发行版随附的/etc/*-release文件.可能是/etc/centos-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.

您可以通过检查哪个文件与centos一起打包来检查适合此代码的/etc/*-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

这是OEL的一个示例,其中/etc/redhat-release被打包为enterprise-release.

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

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

输出:

enterprise-release

这篇关于Linux-Bash-获取$ releasever和$ basearch值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆