-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?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
David
  • 359
  • 2
  • 13

1 Answers1

1

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
    
glenn jackman
  • 84,176
  • 15
  • 116
  • 168