-1

If $IP output equal to any ip address must print 1, else must print 0.

#!/bin/sh

IP=$(/usr/local/bin/dig ns.ripe.net. a +short)

if [ $IP = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" ]; then
   echo 1
elif [ $IP != $ANY_IP ]; then
   echo 0
fi

I need write script,which must check if "/usr/local/bin/dig ns.ripe.net. a +short" output return any ip address,should return 1,else return 0. The purpose of this script is to check dnssec. How can I denote any ip address for comparing?

closed as unclear what you're asking by RalfFriedl, Jeff Schaller, JigglyNaga, user88036, Thomas Oct 11 '18 at 4:29

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    What's happening that you didn't expect? – Jeff Schaller Oct 10 '18 at 13:56
  • 1
    What is the question ? – user88036 Oct 10 '18 at 14:20
2

In increasing order of accuracy:

  1. POSIX shell

    case $IP in 
        *.*.*.*) echo "$IP contains at least 3 dots" ;;
        *) echo "$IP does not contain 3 dots" ;;
    esac
    
  2. bash

    shopt -s extglob
    if [[ $IP == +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]; then
        echo "$IP contains digits separated by dots
    fi
    
  3. bash

    looks_like_IP_address() {
       [[ $1 =~ ^([0-9]+)"."([0-9]+)"."([0-9]+)"."([0-9]+)$ ]] &&
       (( 0 <= ${BASH_REMATCH[1]} && ${BASH_REMATCH[1]} <= 255 )) &&
       (( 0 <= ${BASH_REMATCH[2]} && ${BASH_REMATCH[2]} <= 255 )) &&
       (( 0 <= ${BASH_REMATCH[3]} && ${BASH_REMATCH[3]} <= 255 )) &&
       (( 0 <= ${BASH_REMATCH[4]} && ${BASH_REMATCH[4]} <= 255 ))
    }
    if looks_like_IP_address "$IP"; then
        echo "$IP looks like an IP address"
    fi
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.