9

I'm learning the CLI interface of Advanced Packaging Tool. From the output of apt(8) when its stdout isn't a terminal, it isn't suitable for "scripts expecting stable programming interface", so I'm taking a look at apt-get(8).

One difference between apt update and apt-get update is that the latter is missing a final line after all cache has been updated:

8 packages can be upgraded. Run 'apt list --upgradable' to see them.

I want to know how I can get this exact line displayed with apt-get(8).

  • As far as I knew, apt (Advanced Packaging Tool) and apt-get are different things. They're designed to run differently from what I understood. If one gives you what you want, perhaps make the habit to use that command instead. – Brenden McFarling Mar 30 at 0:49
9

man apt-get shows:

   -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.

So if you just do:

apt-get upgrade --dry-run

it will output:

...
4 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.
...
  • Yeah, I went through man 8 apt-get and found that option, but the output was different from apt. – iBug Mar 25 at 6:35
  • Different how? I just checked on my system and it's identical. I thought it was identical because apt is really a programmatic wrapper around apt-get and that's the reason why the warning exists. – tudor Mar 25 at 6:38
  • apt shows 8 packages can be upgraded. Run 'apt list --upgradable' to see them., while your answer shows 4 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade. – iBug Mar 25 at 6:40
  • 1
    Yes, that's because you have 8 to upgrade where I have 4. Or are you referring to the text being different? – tudor Mar 25 at 6:41
  • 4
    Yes, that's exactly what the warning is about. apt will not guarantee you that text, and they may not even guarantee you that number. apt-get however has a strict output requirement because it's used by other softwares (like UIs and daemons) to process it in various ways. So you can either change your code to accept apt-get's output or you can | sed 's/to upgrade/packages can be upgraded/g', for example (and risk your code breaking later). – tudor Mar 25 at 6:51
4

Guessing you need to handle the number of available updates, here is a suggestion:

# 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

No need to sudo
The output is easy to work with

More options:

> /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)
  • Is this the exact thing used to generate motd on SSH login? – iBug Mar 25 at 7:02
  • yes it looks like the same output, but i dunno how does motd – cmak.fr Mar 25 at 7:07
2

From man 8 apt:

... enables some options ...

Then I went through /usr/share/doc/apt/examples/configure-index.gz (using zcat(1) to show text content) and noticed this option:

apt::cmd::show-update-stats

So I worked out the following command that did exactly what I wanted:

# apt-get -o apt::cmd::show-update-stats=true update

Tested to be working on Xenial and Bionic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.