【问题标题】:apt-get update, dist-upgrade, autoremove, autoclean in a single sudo command在单个 sudo 命令中进行 apt-get update、dist-upgrade、autoremove、autoclean
【发布时间】:2015-01-13 17:50:12
【问题描述】:

我常用的使机器保持最新状态的命令相当冗长,如果任何命令花费很长时间,它可能会导致不止一个密码提示:

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove && sudo apt-get autoclean

我想将其缩短为一个命令(最好不使用全局别名)。

Solution 基于@amra's answeranother tip

sudo sh -c 'apt-get update && apt-get upgrade --yes && if [ -f /var/run/reboot-required ]; then echo You should reboot; fi' 

【问题讨论】:

标签: ubuntu apt-get


【解决方案1】:

试试

sudo sh -c "apt-get -y update;apt-get -y dist-upgrade;apt-get -y autoremove;apt-get -y autoclean"

【讨论】:

  • 人们是否可以指定一个通用名称来创建这样的自定义超级升级命令? (我编造了超级升级这个名字;)
  • 我称之为:alias apt-update-dist-upgrade-autoremove-autoclean='sudo sh -c "apt-get -y update;apt-get -y dist-upgrade;apt-get -y autoremove;apt-get -y autoclean"';但我宁愿只选择apt-get upgrade 并自己决定何时使用dist-upgrade
【解决方案2】:

当且仅当 'cmd1' 没有错误地执行时,才可以使用 '&&' 运算符来执行命令 'cmd2':

(cmd1 && cmd2)

但这只能在 bash 中直接使用,前面没有 'sudo'。

所以,为了按预期工作,我们可以使用以下命令:

sudo /bin/sh -c "apt-get update && apt-get dist-upgrade && apt-get autoremove && apt-get autoclean"

注意amra提出的答案和上面的命令不一样: 用“;”分隔的命令在不考虑前一个命令的退出代码的情况下按顺序执行。 使用“&&”分隔命令时,会考虑退出代码。 因此,如果我们有“cmd1 && cmd2”,则只有在 cmd1 的退出代码为 0 时才会执行 cmd2(即 cmd1 没有失败)。

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2017-05-05
    • 2017-07-04
    相关资源
    最近更新 更多