版本 2

版本 2

我正在编写一个 shell 脚本,它会在我登录时询问我是否要检查系统是否有更新。如果我回答是,它会检查并列出要升级的软件包。然后,它会询问我是否要升级这些软件包。我希望询问我是否要升级软件包的命令仅在有一个或多个需要升级的软件包(列在后面)时运行sudo apt update && apt list --upgradeable。我该怎么做?这是我目前的脚本:

read -r -p "Would you like to check your system for updates? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
sudo apt update && apt list --upgradeable 
read -r -p "Would you like to update your system? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
sudo apt upgrade && sudo apt autoremove && sudo apt autoclean
;;
[nN][oO]|[nN])
clear
;;
*)
clear && echo "Invalid input..."
;;
esac
;;
[nN][oO]|[nN])
clear
;;
*)
clear && echo "Invalid input..."
;;
esac

它基本上需要像这样:如果这段文本在前一个命令的输出中,那么运行下一个命令。

任何帮助我都非常感谢。谢谢!

答案1

使用以下命令获取可用更新的数量:

/usr/lib/update-notifier/apt-check
# returns (for example) 12;4

/usr/lib/update-notifier/apt-check --human-readable
# returns (for example)
  12 packages can be updated.
  4 updates are security updates.

/usr/lib/update-notifier/apt-check |& cut -d";" -f1
# returns (for example) 12

要测试是否有可用的更新,请使用最后一个命令并针对 0 测试其输出。

这是一个可以完成您任务的简单脚本。
请注意,它不处理错误输入,仅捕获“”、“yes”、“YeS”、“y”、“Y”以继续处理。如果没有“yes”输入,则直接退出脚本。

#!/bin/bash

read -r -p "Would you like to check your system for updates? [Y/n] " response
  response=${response,,} # tolower
  if ! ([[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]); then
    exit
  fi
sudo apt update > /dev/null 2>&1
nUpgradables=$(/usr/lib/update-notifier/apt-check |& cut -d";" -f1)

if [ $nUpgradables -gt 0 ]; then
  echo ${nUpgradables}" packages can be updated"
  read -r -p "Would you like to update your system? [Y/n] " input
  response=${response,,} # tolower
  if ! ([[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]); then
    exit
  fi
  sudo apt-get -f install # that is good to do too
  sudo apt upgrade        # upgrade, not 'upgrades'
  sudo apt autoremove
  sudo apt autoclean
else
  echo "No upgrade available"
fi

答案2

#!/bin/bash

# update package list.
aptitude --quiet=2 update

# count upgradeable packages.
read -r c < <(aptitude --quiet=2 search '?narrow(?upgradable, ?not(?action(hold)))' | wc -l)

while true; do
    (( $c <= 0 )) && break
    read -r -p "You have $c upgradable packages, would you like to upgrade? [Y/n] " i
    case ${i,,} in
        [y]|[yes])
            aptitude upgrade # --assume-yes --quiet=2
            break
            ;;
        [n]|[no])
            echo "No action taken..."
            break
            ;;
        *)
            echo "Invalid input..."
            continue
            ;;
    esac
done

exit 0

版本 2

#!/bin/bash

# update package list.
apt-get --quiet=2 update

# count upgradeable packages.
read -r c < <(apt-get --no-act --quiet=2 upgrade | grep -c '^Inst')

while true; do
    (( $c <= 0 )) && break
    read -r -p "You have $c upgradable packages, would you like to upgrade? [Y/n] " i
    case ${i,,} in
        [y]|[yes])
            apt-get upgrade # --assume-yes --quiet=2
            break
            ;;
        [n]|[no])
            echo "No action taken..."
            break
            ;;
        *)
            echo "Invalid input..."
            continue
            ;;
    esac
done

exit 0

相关内容