Linux - Bash - 获取 $releasever 和 $basearch 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20988371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux - Bash - Get $releasever and $basearch values?
提问by Mike Purcell
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:
我正在编写一个 bash 脚本来使用 reposync 从远程仓库拉取包,所以我可以将我的节点指向本地拉取。因此,我试图使本地存储库配置与 usptream 存储库配置尽可能相似,如下所示:
# 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:
在 bash 脚本中,是否有更简洁的方法来获取 $releasever 和 $basearch 值?我正在考虑执行以下操作:
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.
然后对 6/x86_64 值进行正则表达式。有点凌乱,正在寻找更优雅的方法。
回答by alvits
Most distro uses the distroverpkg version to get the releasever and basearch.
大多数发行版使用 distroverpkg 版本来获取 releasever 和 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.
如果您查看 /etc/yum.conf,您会看到 distrover 设置为 redhat-release(对于 RHEL)、enterpriselinux-release(对于 OEL)等。
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.
上面的新代码将尝试获取与文件关联的包/etc/$distro
。一些 Linux 添加/etc/redhat-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
.
如果你得到了,file not owned by any package
那么使用/etc/*-release
你的发行版附带的文件。大概是/etc/centos-release
。
You can check the appropriate /etc/*-release
appropriate for this code by checking which file is packaged with centos.
您可以/etc/*-release
通过检查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
.
这是 OEL 中的一个示例,其中/etc/redhat-release
打包为enterprise-release
.
rpm -q --qf "%{name}" -f /etc/redhat-release
Output:
输出:
enterprise-release