Issue
I noticed today that apt behaves differently depending on where (shell / python / ...) it is run from.
Background I am trying to find out the providing package of "watch" via the apt CLI. This works manually also very well:
apt-get install -s watch
NOTE: This is only a simulation!
apt-get needs root privileges for real execution.
Keep also in mind that locking is deactivated,
so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'procps' instead of 'watch'
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.
The important line is Note, selecting 'procps' instead of 'watch'
: Apparently, watch is provided by procps. So far so good.
When I'm executing the same command from python's subprocess
library, this line disappears. With os.system
, it is working again.
So I've tried to investigate the issue by simply piping the output from my commandline (stdout and stderr) into a log file (apt-get install -s watch &> /tmp/output
)
Result:
NOTE: This is only a simulation!
apt-get needs root privileges for real execution.
Keep also in mind that locking is deactivated,
so don't depend on the relevance to the real current situation!
Reading package lists...
Building dependency tree...
Reading state information...
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.
The line is missing.
After that I searched around a bit in the source code of apt and found the corresponding line. The output stream here is out
, some other ioprintf
sections are using c1out
instead. So this seems to be another stream... But why is my terminal printing the output of this stream and most other programs aren't capable to process it?
Solution
apt behaves differently depending on whether its output is a tty or not.
If its stdout isn't, it behaves as if --quiet=1
had been passed to it.
You can omit the behaviour by explicitely passing --quiet=0
.
apt-get --quiet=0 install -s watch | less
The man-page can tell more about --quiet.
Please note: It is very likely a bad idea to rely on something the apt-developers made harder to pipe or even grep/awk for.
Answered By - Aiyion.Prime