apt-get update、dist-upgrade、autoremove、autoclean 在单个 sudo 命令中
我用来保持机器最新的常用命令相当冗长,如果任何命令需要很长时间,它可能会导致多个密码提示:
sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove && sudo apt-get autoclean
我想将其缩短为一个命令(最好不使用全局别名) )。
sudo sh -c 'apt-get update && apt-get upgrade --yes && if [ -f /var/run/reboot-required ]; then echo You should reboot; fi'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
可以使用“&&”操作符执行命令“cmd2”当且仅当“cmd1”已执行且没有错误:
但这仅适用于直接在 bash 中,前面没有“sudo”。
因此,为了按预期工作,我们可以使用以下命令:
请注意,amra 提出的答案与上面的命令不同:
命令之间用“;”分隔按顺序执行,不考虑前一个命令的退出代码。
使用“&&”时为了分隔命令,需要考虑退出代码。
因此,如果我们有“cmd1 && cmd2”,则仅当 cmd1 的退出代码为 0(即 cmd1 没有失败)时才执行 cmd2。
One can use the '&&' operator to execute command 'cmd2' if and only if 'cmd1' has been executed without errors:
But this only works in bash directly, without 'sudo' in front.
So, in order to work as expected, we can use the following command:
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).