#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Conditional sentence of the first two bits


    Hello, maybe anyone know, how to compare first two/three bits of IP address in conditional sentence?
    for e.g
    I have an ip A:

    A=`192.23.43.132`
    #know i need to check if it is an ip starting by 192.23, so how to determine it by first bits?

    #if [$A= '192.23....'] ???
    then
    echo "true"
    else
    echo "false"
    fi
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Code:
    $ echo 192.23.43.132 | egrep -q '^192\.23'
    $ if [ $? == 0 ]; then echo yes ; fi
    yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo