if [[ `ping -c $count "$HNAME"` ]]
ping in a command substitution (using the old backtick syntax, instead of the saner
$(...)). The resulting output is put on the command line, and then
[[ .. ]], tests to see if the output is empty. (That is, regular output. Command substitution doesn't capture error output, which would still be sent to the script's error output.)
ping outputs anything, the test succeeds. E.g. on my system:
$ ping -c1 188.8.131.52 2>/dev/null
PING 184.108.40.206 (220.127.116.11) 56(84) bytes of data.
--- 18.104.22.168 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
22.214.171.124 is a valid IP address,
ping tries to contact it. With an invalid IP address or a nonexisting hostname, it would only print an error, and the standard output would be empty.
A better way to test if a host is up would be to test the exit status of
ping, and redirect its output away:
if ping -c1 "$host" >/dev/null 2>&1; then
echo "'$host' is up"
echo "'$host' does not respond (or invalid IP address/hostname)"
Note that the quoting in the
echo commands is off:
echo ""$HNAME" seems to be down."
This has an empty quoted string
"", then an unquoted parameter expansion
$HNAME, and then the quoted string
" seems to be down.". For various reasons, it's better to quote all paremeter expansions, so use
"$var blahblah", or
"\"$var\" blahblah" if you want to put quotes around the variable in the output.