When the status code is useless, is there anyway to construct a pipeline based on output from stdout?
I'd prefer the answer not address the use-case but the question in the scope of shell scripting. What I'm trying to do is find the most-specific package available in the repository by guessing the name based on country and language codes.
Take for instance this,
$PACKAGE1=hunspell-en-zz
$PACKAGE2=hunspell-en
The first guess is more appropriate but it may not exist. In this case, I want to return hunspell-en
($PACKAGE2
) because the first option hunspell-en-zz
($PACKAGE1
) does not exist.
pipelines of apt-cache
The command apt-cache
returns success (which is defined by shell as exit-code zero) whenever the command is able to run (from the docs of apt-cache
)
apt-cache returns zero on normal operation, decimal 100 on error.
That makes using the command in a pipeline more difficult. Normally, I expect the package-search equivalent of a 404 to result in an error (as would happen with curl
or wget
). I want to search to see if a package exists, and if not fall back to another package if it exists.
This returns nothing, as the first command returns success (so the rhs in the ||
never runs)
apt-cache search hunspell-en-zz || apt-cache search hunspell-en
apt-cache search
with two arguments
This returns nothing, as apt-cache
ANDs its arguments,
apt-cache search hunspell-en-zz hunspell-en
From the docs of apt-cache
Separate arguments can be used to specify multiple search patterns that are and'ed together.
So as one of those arguments clearly doesn't exist, this returns nothing.
The question
What is the shell idiom to handle conventions like those found in apt-cache
where the return code is useless for the task? And success is determined only by the presence of output on STDOUT?
Similar to
make find fail when nothing was found
they both stemming from the same problem. The chosen answer there mentions
find -z
which sadly isn't applicable solution here and is use-case specific. There is no mention of an idiom or constructing a pipeline without using null-termination (not an option onapt-cache
)
hunspell-en
exists? Anyway, you can useapt-cache policy
and grep for^$PACKAGENAME:
. – AlexP Dec 26 '17 at 23:02hunspell-ar
does exist and there are no country-name packages. I need to find the most accurate package for a given country and language. – Evan Carroll Dec 26 '17 at 23:03apt-cache policy 'hunspell-en*' | grep '^\S'
. – AlexP Dec 26 '17 at 23:05search foo bar
butfoo
doesn't exist, it should printbar
? – nxnev Dec 26 '17 at 23:15find
is just likeapt-cache
in this respect - useless return code, success is based on output. – muru Dec 27 '17 at 03:00-z
which sadly isn't a solution here so the use-case-specific problem isn't applicable. And there is no mention of an idiom or constructing a pipeline without using null-termination (not an option onapt-cache
) – Evan Carroll Dec 27 '17 at 03:21find
to be used with-print0
and so grep with-z
. Since apt-cache isn't giving null-terminated output, you don't need-z
. – muru Dec 27 '17 at 03:55