如何确保 yum install 在 shell 脚本中成功?

发布于 2024-12-20 13:04:58 字数 236 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮茶煮酒煮时光 2024-12-27 13:04:58

我使用了以下方法,这可能不是万无一失的,但似乎有效:

假设变量 PACKAGES 包含您要安装的软件包列表,然后:

  1. 运行 yum -y install $PACKAGES (我假设如果这是一个脚本,您确实希望传递 -y 以避免提示)。
  2. 检查其退出状态以检测一些故障情况。
  3. 运行 rpm --query --queryformat "" $PACKAGES,这对于每个成功安装的软件包不会输出任何内容,并且会输出 package;每次失败都不会安装
  4. 检查其退出状态,这似乎是未成功安装的软件包数量,即成功时将像往常一样为0。

仅当 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:

  1. Run yum -y install $PACKAGES (I assume if this is a script, you really want to pass -y to avoid prompting).
  2. Check its exit status in order to detect some failure conditions.
  3. Run rpm --query --queryformat "" $PACKAGES, which will output nothing for each package that was installed successfully, and will output package <name> is not installed for each failure.
  4. Check its exit status, which appears to be the number of packages that were not successfully installed, i.e. will be 0 on success as usual.

This will only work if PACKAGES contains plain package names that yum is expected to find in a repository, not if it contains other things that yum accepts like URLs, file names or Provides: names.

爱,才寂寞 2024-12-27 13:04:58

基于 此随机帖子< /a>,看起来 yum 返回一个错误代码到 shell。您可以通过运行命令然后立即(作为下一个命令)执行以下操作来测试这一点:

echo $?

这将打印上一个命令的返回代码。成功应该是 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:

echo $?

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. :)

轻许诺言 2024-12-27 13:04:58

通过 ping google.com 不能确保您尝试连接的 yum 存储库可用

检查软件包是否已安装的命令:-

yum list installed PACKAGE_NAME

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 :-

yum list installed PACKAGE_NAME
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文