bash 如何确保 yum 安装在 shell 脚本中成功?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8447011/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:19:09  来源:igfitidea点击:

how to ensure yum install was successful in a shell script?

bashshellyum

提问by jedierikb

I have a shell script which checks if there is an internet connection (by pinging google), and then calls

我有一个 shell 脚本,它检查是否有互联网连接(通过 ping google),然后调用

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)?

我如何确认软件包已安装(或已安装)?我是否再次调用 yum 并解析输出(如果系统使用另一种语言,我认为这会变得非常复杂)?

采纳答案by Dan Fego

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:

根据这个随机帖子,看起来 yum 向 shell 返回了一个错误代码。您可以通过运行命令然后立即(作为下一个命令)执行以下操作来测试它:

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

这将打印上一个命令的返回码。成功应为 0,某种非零失败。但这只是一个猜测,因为我目前没有一个盒子可供我使用。:)

回答by doshea

I've used the following method, which might not be foolproof, but seems to work:

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

Assuming the variable PACKAGEScontains the list of packages you want to install, then:

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

  1. Run yum -y install $PACKAGES(I assume if this is a script, you really want to pass -yto avoid prompting).
  2. Check its exit status in order to detect somefailure 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 installedfor 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.
  1. 运行yum -y install $PACKAGES(我假设这是一个脚本,你真的想通过-y以避免提示)。
  2. 检查其退出状态以检测某些故障情况。
  3. Run rpm --query --queryformat "" $PACKAGES,它不会为每个安装成功的包输出任何内容,并package <name> is not installed为每个失败输出。
  4. 检查它的退出状态,这似乎是没有成功安装的软件包的数量,即像往常一样成功时为 0。

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

这仅在PACKAGES包含yum预期在存储库中找到的普通包名称时才有效,如果它包含其他yum可接受的内容,如 URL、文件名或Provides:名称,则无效。

回答by ajreal

By ping google.com does not ensure the yum repo you trying to connect is available

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

The command to check whether a package is already installed :-

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

yum list installed PACKAGE_NAME