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

时间:2016-04-06 15:10:51

标签: linux bash apt

我需要一个bash行来检查ubuntu包是否需要升级。

例如,我想检查一下这个包' firefox'需要使用dpkg或apt-get命令进行升级。

密码示例:

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

4 个答案:

答案 0 :(得分:2)

这应该适合你:

#!/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

答案 1 :(得分:2)

以下命令可以帮助您: - / usr / lib / update-notifier / apt-check --human-readable

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

有关详细信息,请查看以下链接: -

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

谢谢&问候,
Alok Thaker

答案 2 :(得分:0)

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

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

if [ $? -ne 0 ]
then

 echo "$1 not installed or issue retrieving informations"
 exit

else

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

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

fi

用法:

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

答案 3 :(得分:0)

Bash功能:

apt_needs_upgrade() {

    NEEDS_UPGRADE=$(/usr/lib/update-notifier/apt-check -p 2>&1 >/dev/null | grep "^$1$" | 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;