bash 如何仅获取 yum 更新列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23698638/
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
How to get just a list of yum updates
提问by Mike Q
OK I have always had this problem. I want JUST the available updates listed in a file via bash script from a Linux system (RHEL or Fedora) using yum but I always have to deal with the Header information created which looks like this:
好的,我一直有这个问题。我只想使用 yum 通过 Linux 系统(RHEL 或 Fedora)中的 bash 脚本列出文件中的可用更新,但我总是必须处理创建的标题信息,如下所示:
Loaded plugins: XXXX-repo XXXX-updates
: WWWWWW-repo something-updates QQQQQ-updates
Updated packages
package1.i686 1:234 RHEL 6.5 updates
package2.i686 1:234 RHEL 6.5 updates
package3.i686 1-234 RHEL 6.5 updates
package4.noarch 1.234 RHEL 6.5 updates
All I want is a list of package1,package2, etc. which seems simple enough but it isn't because I can't just grep on "updates" or ":". Am I looking at this wrongly? Why would I not want to capture what updates were found in a script? Should I just update and check what has been updated instead? Thoughts?
我想要的只是包 1、包 2 等的列表,这看起来很简单,但这不是因为我不能只对“更新”或“:”进行 grep。我看错了吗?为什么我不想捕获在脚本中发现的更新?我应该只更新并检查已更新的内容吗?想法?
PS> I can not use --noplugins option.
PS> 我不能使用 --noplugins 选项。
EDIT: So far I have come up with this,
编辑:到目前为止,我已经想出了这个,
sudo yum check-update | grep "\." | awk '(NR >=1) {print ;}' | grep '^[[:alpha:]]'
Basically grab the lines with a period in them, the first line, and make sure it first contains alpha letters. Perhaps over done but it seems to work.
基本上抓住其中有句点的行,第一行,并确保它首先包含字母。也许完成了,但它似乎有效。
回答by glenn Hymanman
To only print lines following (but not including) "Updated packages"
只打印以下(但不包括)“更新包”的行
yum check-update | awk 'p; /Updated packages/ {p=1}'
Note, on my Fedora system, a blank line separates the "header" from the list of updatable packages, so I would use awk 'p;/^$/{p=1}'
请注意,在我的 Fedora 系统上,一个空行将“标题”与可更新包列表分开,所以我会使用 awk 'p;/^$/{p=1}'
回答by Jonathan Wheeler
If you pipe the output above into awk using this command:
如果您使用以下命令将上面的输出通过管道传输到 awk:
| awk '(NR >=4) {print ;}'
You will get the following output
您将获得以下输出
package1.i686
package2.i686
package3.i686
package4.noarch
The (NR >=4) tells awk to ignore the first three lines. The {print $1;} tells awk to print the first word of each line.
(NR >=4) 告诉 awk 忽略前三行。{print $1;} 告诉 awk 打印每一行的第一个单词。
You can read herefor more information on cutting stuff out after certain characters on each line. You can then use sed if stripping out everything after the . is important
您可以在此处阅读有关在每行的某些字符后删除内容的更多信息。如果在 . 很重要
| awk '(NR >=4) {print ;}' | sed s/\.[^\.]*$//
Gives the following output
给出以下输出
package1
package2
package3
package4
Then pipe it into another sed command to replace the linebreakswith a comma.
然后将其通过管道传输到另一个 sed 命令以用逗号替换换行符。
| awk '(NR >=4) {print ;}' | sed s/\.[^\.]*$// | sed ':a;N;$!ba;s/\n/,/g'
Yields the following output
产生以下输出
package1,package2,package3,package4
回答by Tiago Lopo
Try this:
尝试这个:
yum check-update | awk '{if( ~ /updates/){print }}' | tr '\n' ','
If the input contains 'updates' on fifth column then print first column and create a csv list.
如果输入在第五列中包含“更新”,则打印第一列并创建一个 csv 列表。
回答by ePi272314
A more flexible solution
更灵活的解决方案
The solution below does?not assume a specific number of lines in the Header (Ex. in CentOS I got much more header lines).
Nor does it suppose that you are only interested in the repository updates
.
下面的解决方案不假设标题中有特定数量的行(例如,在 CentOS 中,我有更多的标题行)。
它也不假设您只对存储库感兴趣updates
。
yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }' > updates
yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }' > updates
Example
例子
For the following yum check-update
output
对于以下yum check-update
输出
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 31 kB 00:00:00
* base: asi-fs-m.net
Excluding mirror: mirror.de.leaseweb.net
Excluding mirror: mirror.fra10.de.leaseweb.net
base | 3.6 kB 00:00:00
cwp | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
mariadb | 2.9 kB 00:00:00
remi-safe | 3.0 kB 00:00:00
updates | 3.4 kB 00:00:00
remi-safe/primary_db | 1.4 MB 00:00:00
openvpn.x86_64 2.4.7-1.el7 epel
polkit.x86_64 0.112-18.el7_6.1 updates
pure-ftpd.x86_64 1.0.47-2.el7 epel
remi-release.noarch 7.6-2.el7.remi remi-safe
You can get
你可以得到
openvpn.x86_64
polkit.x86_64
pure-ftpd.x86_64
remi-release.noarch
Explanation
解释
This solution assumes that the relevant lines have the pattern<package name><spaces><version number><spaces><repo name>
此解决方案假定相关行具有模式<package name><spaces><version number><spaces><repo name>
If you want to output?a particular?repository, then use the pattern/\S+\s+[0-9]\S+\s+repo_name/
如果你想输出?一个特定的?存储库,然后使用模式/\S+\s+[0-9]\S+\s+repo_name/
PS:
PS:
If this solution does not work in your system, let me know in a comment
如果此解决方案在您的系统中不起作用,请在评论中告诉我