functions/__fish_print_hostnames: Check getent's status

This previously effectively checked `string split ' '`s return status,
which was false if it didn't split anything. And while that should be
true if getent fails (because it should produce no output), it's also
true if it doesn't print a line with multiple aliases. Which should be
fairly typical.

Instead we use our new-found $pipestatus to check what getent returns,
in the assumption that it'll fail if it doesn't support hosts.

Follow up to 8f7a47547e.

[ci skip]
This commit is contained in:
Fabian Homborg 2019-06-25 08:38:12 +02:00
parent 9cd29e5166
commit 3bc392a6b3

View file

@ -8,7 +8,8 @@ function __fish_print_hostnames -d "Print a list of known hostnames"
type -q getent
# Ignore zero IPs.
and getent hosts 2>/dev/null | string match -r -v '^0.0.0.0' | string replace -r '^\s*\S+\s+' '' | string split ' '
or if test -r /etc/hosts
# We care about _getent_s status, not `string split`s.
if test $pipestatus[1] -ne 0; and test -r /etc/hosts
# Ignore commented lines and functionally empty lines.
string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' </etc/hosts | string replace -r -a '#.*$' '' | string replace -r '^\s*\S+\s+' '' | string trim | string replace -r -a '\s+' ' ' | string split ' '
end