How can i check if my host command in script got a valid ip

Thodoris Diamantidis

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 -

jesse_b

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I check if cgroups are available on my Linux host?

From Dev

How can I check what is my ip on any particular endpoint?

From Dev

How can I hide my IP from linux who command

From Dev

How can I get my external IP address in a shell script?

From Dev

How can I parameterise my command in a bash script file?

From Dev

If my server has multiple IP addresses, how can I run a script using each of the IP addresses?

From Dev

How can I host my own website

From Dev

How can I host my own website

From Dev

How can I get my public IP address from the command line, if I am behind a router?

From Dev

How can i see if my app got iCloud data?

From Dev

How can I make my PHP script log the IP of all visiters?

From Java

How can i check whether an element can host Shadow DOM?

From Dev

How can I check the build command for my JavafX app in IntelliJ IDEA

From Dev

How can I check from the command-line if my integrated Wi-Fi adapter is disabled?

From Dev

How can I check that a file is a valid HDF5 file?

From Dev

How can I check that an NSData blob is valid as resumeData for an NSURLSessionDownloadTask?

From Dev

How can I check for invalid input and loop until the input is valid?

From Dev

How can I best check these Elliptic Curve parameters are valid?

From Dev

How can I check for the actual time in a Script?

From Dev

How i can run Ghost Script command

From Dev

How can I check if my AVPlayer is buffering?

From Dev

How can I check RAM of my laptop?

From Dev

How can I debug my command controller?

From Dev

Can i check if a json got a determinated structure?

From Dev

How can I get a valid Authenticity Token with my Rails Console?

From Dev

Can someone check my sql is valid [postgresql]

From Dev

How can I improve the performance of my script?

From Dev

How can I fix my Bash script?

From Dev

How do I get my IP address from the command line?

Related Related

  1. 1

    How can I check if cgroups are available on my Linux host?

  2. 2

    How can I check what is my ip on any particular endpoint?

  3. 3

    How can I hide my IP from linux who command

  4. 4

    How can I get my external IP address in a shell script?

  5. 5

    How can I parameterise my command in a bash script file?

  6. 6

    If my server has multiple IP addresses, how can I run a script using each of the IP addresses?

  7. 7

    How can I host my own website

  8. 8

    How can I host my own website

  9. 9

    How can I get my public IP address from the command line, if I am behind a router?

  10. 10

    How can i see if my app got iCloud data?

  11. 11

    How can I make my PHP script log the IP of all visiters?

  12. 12

    How can i check whether an element can host Shadow DOM?

  13. 13

    How can I check the build command for my JavafX app in IntelliJ IDEA

  14. 14

    How can I check from the command-line if my integrated Wi-Fi adapter is disabled?

  15. 15

    How can I check that a file is a valid HDF5 file?

  16. 16

    How can I check that an NSData blob is valid as resumeData for an NSURLSessionDownloadTask?

  17. 17

    How can I check for invalid input and loop until the input is valid?

  18. 18

    How can I best check these Elliptic Curve parameters are valid?

  19. 19

    How can I check for the actual time in a Script?

  20. 20

    How i can run Ghost Script command

  21. 21

    How can I check if my AVPlayer is buffering?

  22. 22

    How can I check RAM of my laptop?

  23. 23

    How can I debug my command controller?

  24. 24

    Can i check if a json got a determinated structure?

  25. 25

    How can I get a valid Authenticity Token with my Rails Console?

  26. 26

    Can someone check my sql is valid [postgresql]

  27. 27

    How can I improve the performance of my script?

  28. 28

    How can I fix my Bash script?

  29. 29

    How do I get my IP address from the command line?

HotTag

Archive