vendredi 4 mars 2016

bash - if command output is NOT null, write output to a file

I feel like this should be very, very, easy... but somehow I can't figure it out.

The matter is simple. I have the following if statement, which I'm using to check IPs and see if they have a reverse DNS or not. If there is a valid Reverse DNS available, I write it to a file. If not, I write "NXDOMAIN" to the same file.

if [[ $(dig +noall +answer -x 10.10.10.10 | grep -m 1 PTR | awk '{ print $5 }') ]]; then
       dig +noall +answer -x 10.10.10.10 | grep -m 1 PTR | awk '{ print $5 }' > /root/IP
else
       echo "NXDOMAIN" > /root/IP
fi

This works OK, but it runs the command twice, unnecessarily. Is there a way in which, if the command returns a valid reverse DNS, I can output it directly to a file, without running the command again?

So, basically, something like this:

if [[ $(dig +noall +answer -x 10.10.10.10 | grep -m 1 PTR | awk '{ print $5 }') ]]; then
       <write output of the above command in the "/root/IP" file>
else
       echo "NXDOMAIN" > /root/IP
fi

Aucun commentaire:

Enregistrer un commentaire