Suppose we only have 1 parameter
host $1
How can I check if the above command will give me an answer or:
Host $1 not found (NXDOMAIN)
and instead of showing an error not found, I would like to show just a -
host
will exit 1 if it is unable to resolve your input so you can use an if construct:
if ! host "$1" | grep -v 'not found'; then
printf '%s\n' '-'
fi
We are using !
to test if the command fails (since I assume you want to just print the output as normal if it passes). host
sends the not found
error to stdout instead of stderr so we can't just hide stdout. This is why I am piping the host command into grep -v 'not found'
this will hide the not found error if it exists and still exit 1, causing the if construct to print -
. If it does not find not found
it will exit 0.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments