Bash if 条件检查 ubuntu 包是否有更新的版本?

Bash if condition to check if a ubuntu package has a newer version?

我需要 bash 行来检查 ubuntu 软件包是否需要升级。

例如,我想使用 dpkg 或 apt-get 命令检查包 'firefox' 是否需要升级。

假设示例:

# Hypothetical example pseudo-code
if [[ $(firefox_needs_upgrade) ]]; then echo "Firefox needs upgrading";fi

好的,用经过测试的代码编辑了我的答案。这可能是一种更漂亮的方式:

#retrieving info about the package
dpkg -l  >/dev/null

if [ $? -ne 0 ]
then

 echo " not installed or issue retrieving informations"
 exit

else

 #We check if pkg is upgradable
 UPGRADABLE=$(apt-get -s upgrade -u | egrep -c "^Conf  |^Inst  ")

 if [ $UPGRADABLE -gt 0 ]
 then
  echo " need an upgrade"
 else
  echo " up to date"
 fi

fi

用法:

# /tmp/check.sh openssh-server
openssh-server need an upgrade

这应该适合你:

#!/bin/bash
PACKAGE="firefox"
TMP_FILE="/tmp/upgrade_list"

apt-get -yqq update
apt-get -sqq upgrade | grep Inst > ${TMP_FILE}
grep -qi ${PACKAGE} ${TMP_FILE}

if [ $? == 0 ]
then
   echo "${PACKAGE} needs to be upgraded"
fi

以下命令会对您有所帮助:-
/usr/lib/update-notifier/apt-check --人类可读

猫 /var/lib/update-notifier/updates-available

有关详细信息,您可以查看下面的 link :-

https://askubuntu.com/questions/49958/how-to-find-the-number-of-packages-needing-update-from-the-command-line

感谢和问候,
阿洛克塞克

Bash函数:

apt_needs_upgrade() {

    NEEDS_UPGRADE=$(/usr/lib/update-notifier/apt-check -p 2>&1 >/dev/null | grep "^$" | wc -l)

    if [ "$NEEDS_UPGRADE" == 1 ]; then
        return 0; # 0 means true in bash!!!
    else
        return 1; # false
    fi;
}

使用它:

if apt_needs_upgrade "firefox"; then
    echo "Needs upgrading"
else
    echo "No need to upgrade"
fi;