Solved

BASH - script problem

Posted on 2007-04-07
9
312 Views
Last Modified: 2013-12-26
h=0
while [ $h -eq 0 ]; do
if echo "$host" | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$';
then
IFS='.'
arr=($host)
if [ ${arr[0]} -le 255 -a ${arr[1]} -le 255 -a ${arr[2]} -le 255 -a ${arr[3]} -le 255 ];
then
if [ ${arr[0]:0:1} !-eq 0 ] || [ ${arr[1]:0:1} !-eq 0 ] || [ ${arr[2]:0:1} !-eq 0 ]; then
h=1
IFS=''
fi
else
echo "Error: The maximum number you can write is 255, try again."
read host
fi
else
echo "Error: You MUST use only digits from 1 to 5, and the ip must be like xxx.xxx.xxx.xxx, try again."
read host
fi
done

Any ideea whats wrong in this ?
Should i put IFP='' after each else or something ? :|
0
Comment
Question by:rares_dumitrescu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18869092
h=0
while [ $h -eq 0 ]; do
  if echo "$host" | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$';
  then
    IFS='.'
    arr=($host)
    if [ ${arr[0]} -le 255 -a ${arr[1]} -le 255 -a ${arr[2]} -le 255 -a ${arr[3]} -le 255 ];
    then
      if [ ! ${arr[0]:0:1} -eq 0 ] || [ ${arr[1]:0:1} -ne  0 ] || [ ! ${arr[2]:0:1} -eq 0 ]; then
        h=1
        IFS=''
      fi
    else
      echo "Error: The maximum number you can write is 255, try again."
      read host
    fi
  else
    echo "Error: You MUST use only digits from 1 to 5, and the ip must be like xxx.xxx.xxx.xxx, try again."
    read host
  fi
done
0
 

Author Comment

by:rares_dumitrescu
ID: 18869134
it works if i put: 123.0.123.123
any sugestions ? i want to check if the ip typed is like: xxx.xxx.xxx.xxx (x, xx, or xxx) and if the first number of every xxx isn't 0

Example: 12.237.2.124 -> correct
                73.02.21.14 -> incorrect
                84.14.013.23 -> incorrect

did you get the ideea?
thanx
0
 
LVL 84

Expert Comment

by:ozo
ID: 18870252
h=0
while [ $h -eq 0 ]; do
  if echo "$host" | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$';
  then
    IFS='.'
    arr=($host)
    if [ ${arr[0]} -le 255 -a ${arr[1]} -le 255 -a ${arr[2]} -le 255 -a ${arr[3]} -le 255 ]
    then
      if [ ${arr[0]:0:1} -ne 0 -a ${arr[1]:0:1} -ne  0 -a ${arr[2]:0:1} -ne 0 ] ; then
        h=1
        IFS=''
      else
        echo "Error: The first digit cannot be 0, try again"
        read host
      fi
    else
      echo "Error: The maximum number you can write is 255, try again."
      read host
    fi
  else
    echo "Error: You MUST use only digits from 1 to 5, and the ip must be like xxx.xxx.xxx.xxx, try again."
    read host

fi
done
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:nixfreak
ID: 18870368
h=0
while [ $h -eq 0 ]; do
        read host
        echo $host | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' >
/dev/null
        if [ "$?" -ne 0 ] ; then
                echo "Error: The ip must be like xxx.xxx.xxx.xxx, try again."
                continue
        else
                IFS='.'
                arr=($host)
                for i in 0 1 2 3 ; do
                        if [ ${arr[i]} -gt 255 ] ; then
                                echo "Error: The maximum number you can write is 255, try again"
                                continue 2
                        fi
                        echo ${arr[i]} | egrep "0[0-9]+" > /dev/null
                        if [ "$?" -eq 0 ] ; then
                                echo "Error: The first digit cannot be 0, try again"
                                continue 2
                        fi
                done
        fi
        h=1 ; echo "IP accepted!"
done
0
 
LVL 7

Assisted Solution

by:nixfreak
nixfreak earned 100 total points
ID: 18870374
Typo.

h=0
while [ $h -eq 0 ]; do
        read host
        echo $host | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' > /dev/null
        if [ "$?" -ne 0 ] ; then
                echo "Error: The ip must be like xxx.xxx.xxx.xxx, try again."
                continue
        else
                IFS='.'
                arr=($host)
                for i in 0 1 2 3 ; do
                        if [ ${arr[i]} -gt 255 ] ; then
                                echo "Error: The maximum number you can write is 255, try again"
                                continue 2
                        fi
                        echo ${arr[i]} | egrep "0[0-9]+" > /dev/null
                        if [ "$?" -eq 0 ] ; then
                                echo "Error: The first digit cannot be 0, try again"
                                continue 2
                        fi
                done
        fi
        h=1 ; echo "IP accepted!"
done       
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 18870385
the first test could also have been
  if echo "$host" | egrep -e '^[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}$';
but that does not show whats wrong in the original script
0
 

Author Comment

by:rares_dumitrescu
ID: 18872188
yes ozo, finally, "if echo "$host" | egrep -e '^[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}$';"

please, if you can explain what [1-9][0-9]{0,2} or [1-9]{1,3} means, so i can learn, maybe i will need it again sometime.

thanx a lot, and have a HAPPY EASTER :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 18872665
[1-9] means any character fron '1' to '9'
[1-9]{1,3} means any character fron '1' to '9' between 1 and 3 times
][0-9]{0,2} means any character fron '0' to '9' between 0 and 2 times
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new('^[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}$')->explain"
0
 

Author Comment

by:rares_dumitrescu
ID: 18872844
thanx mr ozo
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
monitor and log every file access on redhat/linux 4 143
has22 challenge 11 107
unix example issues 18 106
Problem to App 4 118
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question