I have the following function that prints my current IP:
function get_ip
echo -n -s (ifconfig en1 | grep inet | grep -v inet6 | awk '{print $2}')
end
$ get_ip
192.168.1.4~
I'm looking forward to print offline
if get_ip
returns nothing (it prints "" when I'm offline).
I tried:
if get_ip
echo "Yes"
else
echo "No"
end
But it always returns Yes
.
How can I accomplish this?
You need to check the exit status of the test builtin.
if test -n (get_ip)
echo Yes
else
echo No
end
While testing, I found some inconsistency between -n
and -z
that I will follow up on. Try this:
if test -z (get_ip)
echo No
else
echo Yes
end
According to this answer from the fish maintainer, both answers are wrong, even though the 2nd one works. Use either
count (get_ip); and echo Y; or echo N
or
set result (get_ip)
test -n "$result"; and echo Y; or echo N