Thank you for the example @Matt-Kita.
I'd be curious what \dig crctes10
shows.
Backslash to elide aliases and elide +short because I'd be curious what the dig messages show.
This seems like it may be search domain related. But crctes10 obviously resolves to an IP address; 172.30.161.36.
I've never seen dig use different search domain settings than nslookup. At least when it comes to /etc/resolv.conf.
Is there a chance that there's something in an alias that is causing dig to not surface errors?
I think that a tcpdump of dig's traffic and nslookup's traffic would be very interesting. But I assume that you can't / won't disclose that for obvious security reasons. -- Nice use of sed to redact data.
Just some picky stuff to make better use of shell resources:
<Command>
if [ $? == 0 ] ; then
...
fi
Can be reduced to:
if <Command> ; then
...
fi
Command can be an attribution too:
if var=$(<Command>) ; then
...
fi
We can avoid useless use of cat by replacing:
for <Var> in $(cat <File>)
...
done
By
while read <Var> ; do
...
done < <File>
Finally, a conditional continue
statement can skip the loop for names without IPs:
ip=$(getent ahostsv4 $name) || continue
All that said, my version:
while read name ; do
ip=$(getent ahostsv4 $name) || continue
ip=$(awk '{ print $1 ; exit }' <<< "$ip")
if ping -4qc 1 $name &> /dev/null ; then
output='Pingable'
else
output='Not Pingable'
fi
printf 'Queue Name: %s IP Address: %s Ping Output: %s."\n' "$name" "$ip" "$output"
done < prtqueue
I agree with the technical accuracy of @EmersonPrado comment.
However I've found that I prefer to use the forms that he is suggesting to avoid.
I believe that if [ $? == 0 ] ; then
is fairly self self explanatory that $?
(return code) is being compared ==
to zero 0
. Conversely, you need to be aware of and remember that if <Command> ' then
is both checking the return code of <Command>
and that return codes of 0
evaluate as true thus satisfying the if
condition.
As I've taught junior admins over the years, I vary rarely had someone ask what if [ $? == 0 ]; then
did. I frequently had someone ask what if <Command>; then
did.
As for useless use of cat, I counter with Useful Uses of cat
I've often seen people cat a file as they build up a more complex command. I personally believe that retroactively altering it to remove a so called useless use of cat to be questionable a best. -- This seems to be especially germane for junior admins that are learning how to function / thrive in the shell.
I believe that it behooves us to encourage junior admins to spend time at the shell combining commands and getting results that better match their use case. I think that it's a disservice to them to nit-pick about using a command that isn't technically necessary. I similarly believe that doing that to junior admins is a disservice to all admins that can -> will benefit from the junior admins coming up through the ranks.
But all means, teach the junior admins the neat tricks and things they don't necessarily need to do. But do it as trivia. Please don't do it as a nitpick.