I am writing a script that needs to read a file containing information of a package for which I wrote this line
apt show $PACKAGE_NAME > pack_info.txt
However this doesn't creates the pack_info.txt
file and always gives this Warning :
WARNING : apt does not have a stable CLI interface. Use with caution in scripts.
For the time being I tried redirecting both STDOUT
& STDERR
using &>
and it worked out to give a file pack_info.txt
that we need.
I also tried getting contents from dpkg
and that also worked :
dpkg -s $PACKAGE_NAME > pack_info.txt
What is good that it neither shows a warning nor an error.
What is bad that we don't want to use dpkg and only want STDOUT of apt to redirect to file.
So, I have three Questions to ask :
- What do we exactly mean by Stable CLI Interface ?
- How to safely and error-free use such commands in scripts ?
[please care to explain with example] - Is there a way to only and only redirect STDOUT of
apt show
to a file ?
Best Answer
apt
is for the terminal and gives beautiful output whileapt-get
andapt-cache
are for scripts and give stable, parsable output. The script equivalent of yourapt show
command therefore is:Now to answer your questions one by one:
apt
's output is not well usable in scripts. For example,apt install
(compared toapt-get install
) displays a progress bar that's useless for scripts and can throw errors when the output is parsed.apt show firefox
shows a hint for an additional record, which is also totally useless in a script, you want it to simply output every record there – that's whatapt-cache show firefox
does. Let's see whatman apt
has to say about that:Just use
apt-get
orapt-cache
respectively instead of plainapt
. :) See this answer for a list of equivalents.You did that correctly already:
>file
or1>file
redirects stdout,2>file
redirects stderr and&>file
redirects both tofile
.