添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
乖乖的拐杖  ·  CefSharp ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My usual command for keeping the machine up to date is rather verbose, and it can result in more than one password prompt if any command takes a long time:

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

I'd like to shorten this down to one command (preferably without using a global alias).

Solution based on @amra's answer and another tip:

sudo sh -c 'apt-get update && apt-get upgrade --yes && if [ -f /var/run/reboot-required ]; then echo You should reboot; fi' 
                try: sudo -s then use: apt-get update && apt-get dist-upgrade && apt-get autoremove && apt-get autoclean; logout
– Javaguru
                Jul 23, 2010 at 8:56
                Is there a common name people may assign to creating a custom mega-upgrade command like this? (I made up the name mega-upgrade ;)
– Mikeumus
                Jan 16, 2015 at 11:52
                I would call it: 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"'; But I would prefer only apt-get upgrade and decide myself when to dist-upgrade.
– rubo77
                Jan 21, 2015 at 10:47

One can use the '&&' operator to execute command 'cmd2' if and only if 'cmd1' has been executed without errors:

(cmd1 && cmd2)

But this only works in bash directly, without 'sudo' in front.

So, in order to work as expected, we can use the following command:

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

Note that the answer proposed by amra does not the same as the above command: Commands separated by ";" are executed in sequence without taking the exit code of the previous command into account. When using "&&" to separate the commands, the exit code is taken into account. Thus, if we have "cmd1 && cmd2", cmd2 is only executed if the exit code of cmd1 was 0 (i.e. cmd1 did not fail).

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.