使“apt-get update”将确切输出显示为“apt update”

iBu*_*Bug 8 apt

我正在学习 Advanced Packaging Tool 的 CLI 界面。从apt(8)它的 stdout 不是终端时的输出来看,它不适合“需要稳定编程接口的脚本”,因此我正在查看apt-get(8).

apt update和之间的一个区别apt-get update是后者在所有缓存更新后缺少最后一行:

8 packages can be upgraded. Run 'apt list --upgradable' to see them.
Run Code Online (Sandbox Code Playgroud)

我想知道如何使用apt-get(8).

tu-*_*duh 9

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.
Run Code Online (Sandbox Code Playgroud)

所以如果你只是这样做:

apt-get upgrade --dry-run

它会输出:

...
4 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.
...
Run Code Online (Sandbox Code Playgroud)

  • 是的,这正是警告的内容。`apt` 不会向您保证该文本,他们甚至可能无法向您保证该数字。然而,`apt-get` 有一个严格的输出要求,因为它被其他软件(如 UI 和守护进程)用于以各种方式处理它。所以你可以改变你的代码来接受`apt-get`的输出,或者你可以`| sed 's/to upgrade/packages can be upgrades/g'`,例如(然后冒着代码破坏的风险)。 (4认同)

iBu*_*Bug 2

man 8 apt

...启用一些选项...

然后我浏览/usr/share/doc/apt/examples/configure-index.gz(用于zcat(1)显示文本内容)并注意到这个选项:

apt::cmd::show-update-stats
Run Code Online (Sandbox Code Playgroud)

所以我制定了以下命令,它完全满足了我的要求:

# apt-get -o apt::cmd::show-update-stats=true update
Run Code Online (Sandbox Code Playgroud)

经测试可在 Xenial 和 Bionic 上运行。