AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1171462
Accepted
GNULinuxOnboard
GNULinuxOnboard
Asked: 2019-09-07 22:15:16 +0800 CST2019-09-07 22:15:16 +0800 CST 2019-09-07 22:15:16 +0800 CST

如果上一个命令的输出具有特定值,则运行命令

  • 772

我正在编写一个 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

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

任何帮助深表感谢。谢谢!

scripts command-line updates upgrade
  • 2 2 个回答
  • 128 Views

2 个回答

  • Voted
  1. cmak.fr
    2019-09-07T22:43:29+08:002019-09-07T22:43:29+08:00

    通过以下方式获取可用更新的数量:

    /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' 以继续进行。如果没有“是”输入,则只需退出脚本。

    #!/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
  2. Best Answer
    user986805
    2019-09-22T07:54:43+08:002019-09-22T07:54:43+08:00
    #!/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
    
    • 1

相关问题

  • 从 8.04 LTS 升级到 10.04 LTS 的体验?

  • 分销升级的合理途径

  • 如何让 Ubuntu 减少检查更新的频率?对于 11.04 及更高版本对于赶时间的人!通过 Ubuntu 软件中心

  • 如何启用自动更新?

  • 在不使用标准升级系统的情况下升级有哪些替代方案?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve