Centos

當包不可用時 yum 應該出錯

  • July 24, 2018

我最近在嘗試執行以下操作的腳本中遇到了一個錯誤:

yum -y install another_package.x86_64 some_package.x86_64 && run_my_script

該腳本在較新的 CentOS 上執行良好,但是當我嘗試在 Cent OS 5 上執行它時,它some_package.x86_64不可用。但不是錯誤和停止yum只是列印消息:

No package some_package.x86_64 available.

如何yum在這種情況下強制出錯(哪個 IMO 應該是預設值)以使我的腳本更健壯?

正如您所發現的,這種行為在 RHEL 5 和 6 之間發生了變化(有關一些討論,請參閱https://bugzilla.redhat.com/show_bug.cgi?id=736694)。從該連結,檢查返回程式碼yum info <pkg>應該允許您根據需要中止腳本。就像是:

# Set a variable containing the packages to install:
pkgs_to_install='another_package.x86_64 some_package.x86_64'

# Loop over the packages in the list:
for pkg in ${pkgs_to_install}; do
 # Stop executing if at least one package isn't available:
 yum info ${pkg} >> /dev/null 2>&1 || exit
done

# Continue running your original script:
yum -y install ${pkgs_to_install} && run_my_script

引用自:https://serverfault.com/questions/694942