如何确保 yum install 在 shell 脚本中成功?
我有一个 shell 脚本,它检查是否有互联网连接(通过 ping google),然后调用
yum install packageA packageB --assumeyes
How will I recognize that the packages were displayed(或已经安装)?我是否再次进行 yum 调用并解析输出(我认为如果系统使用另一种语言,这会变得非常复杂)?
I have a shell script which checks if there is an internet connection (by pinging google), and then calls
yum install packageA packageB --assumeyes
How would I confirm that the packages were installed (or were already installed)? Do I make another yum call and parse the output (I presume this gets very complicated if the system is in another language)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用了以下方法,这可能不是万无一失的,但似乎有效:
假设变量
PACKAGES
包含您要安装的软件包列表,然后:yum -y install $PACKAGES
(我假设如果这是一个脚本,您确实希望传递-y
以避免提示)。package;每次失败都不会安装
。仅当
PACKAGES
包含yum
预计在存储库中找到的纯包名称时才有效,如果它包含yum
接受的其他内容则无效例如 URL、文件名或Provides:
名称。I've used the following method, which might not be foolproof, but seems to work:
Assuming the variable
PACKAGES
contains the list of packages you want to install, then:yum -y install $PACKAGES
(I assume if this is a script, you really want to pass-y
to avoid prompting).rpm --query --queryformat "" $PACKAGES
, which will output nothing for each package that was installed successfully, and will outputpackage <name> is not installed
for each failure.This will only work if
PACKAGES
contains plain package names thatyum
is expected to find in a repository, not if it contains other things thatyum
accepts like URLs, file names orProvides:
names.基于 此随机帖子< /a>,看起来 yum 返回一个错误代码到 shell。您可以通过运行命令然后立即(作为下一个命令)执行以下操作来测试这一点:
这将打印上一个命令的返回代码。成功应该是 0,失败应该是非零。但这只是一个猜测,因为我目前没有可用的盒子。 :)
Based on this random post, it looks like yum returns an error code to the shell. You can test this out by running a command and then immediately (as the next command) doing:
That will print the previous command's return code. Success should be 0, failure of some kind nonzero. But that's just a guess since I don't have a box accessible to me at the moment. :)
通过 ping google.com 不能确保您尝试连接的 yum 存储库可用
检查软件包是否已安装的命令:-
By ping google.com does not ensure the yum repo you trying to connect is available
The command to check whether a package is already installed :-