我正在学习 Advanced Packaging Tool 的 CLI 接口。从其 stdout 不是终端时的输出来看apt(8)
,它不适合“需要稳定编程接口的脚本”,所以我正在研究apt-get(8)
。
apt update
和之间的一个区别apt-get update
是后者在所有缓存都更新后缺少最后一行:
8 packages can be upgraded. Run 'apt list --upgradable' to see them.
我想知道如何才能显示这一精确的线条apt-get(8)
。
答案1
man apt-get
显示:
-s, --simulate, --just-print, --dry-run, --recon, --no-act
No action; perform a simulation of events that would occur based on
the current system state but do not actually change the system.
Locking will be disabled (Debug::NoLocking) so the system state
could change while apt-get is running. Simulations can also be
executed by non-root users which might not have read access to all
apt configuration distorting the simulation. A notice expressing
this warning is also shown by default for non-root users
(APT::Get::Show-User-Simulation-Note). Configuration Item:
APT::Get::Simulate.
所以如果你这样做:
apt-get upgrade --dry-run
它将输出:
...
4 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.
...
答案2
猜测您需要处理可用更新的数量,这里有一个建议:
# With no option, returns two numbers, no CR nor LF
/usr/lib/update-notifier/apt-check
# With --human-readable, returns numbers, locale LANG text & CR/LF
/usr/lib/update-notifier/apt-check --human-readable
不需要 sudo
输出很容易使用
更多的选择:
> /usr/lib/update-notifier/apt-check --help
Usage: apt-check [options]
Options:
-h, --help show this help message and exit
-p, --package-names Show the packages that are going to be
installed/upgraded
--human-readable Show human readable output on stdout
--security-updates-unattended
Return the time in days when security updates are
installed unattended (0 means disabled)
答案3
从man 8 apt
:
... 启用一些选项...
然后我浏览/usr/share/doc/apt/examples/configure-index.gz
(用于zcat(1)
显示文本内容)并注意到这个选项:
apt::cmd::show-update-stats
因此我制定出了以下命令来完全实现我的要求:
# apt-get -o apt::cmd::show-update-stats=true update
经过测试,可以在 Xenial 和 Bionic 上运行。