The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help on egrep arund_01 Shell Programming and Scripting 2 05-13-2008 10:26 AM
egrep help Darklight Shell Programming and Scripting 1 04-17-2008 11:34 PM
egrep DNAx86 Shell Programming and Scripting 7 01-18-2008 04:59 AM
Egrep cheat sheet anywhere? Looking for meaning of egrep -c leelm UNIX for Dummies Questions & Answers 2 01-11-2008 11:37 AM
egrep help Vozx Shell Programming and Scripting 2 12-09-2005 06:42 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-10-2004
Registered User
 

Join Date: Oct 2004
Posts: 5
Egrep Help

I'm writing a small script thats purpose is to validate a single command line argument to make sure it is an integer. Also acceptable are a leading "+" or "-", but no more than one.

Example: "5" "-2" "+4" are all valid

If its invalid I simply print out a message saying so, otherwise I just return the value (I need to remove the leading "+" though).

Here's the code I have so far:

Code:
# Checks for valid number of arguments
# Exits program with return code of 1
if [ $# -ne 1 ]; then
        echo "Invalid number of arguments; supports only one argument"

        exit 1
fi

# Checks for valid argument
echo $1 | egrep -s "^[+|-]?[0-9]+$"

# If argument is valid then return its value
# Exits program with return code of 0
if [ $? -eq 0 ]; then
        # Removes a leading '+' sign if there is one
        tr -d '+' $1
        echo $1

        exit 0
# If argument is invalid then return an error message
# Exits program with return code of 2
else
        echo "Invalid operand: $1"

        exit 2
fi
My problem is that when a valid argument is entered the cursor just moves to the next line and sits there. I'm guessing something must be wrong with my eGrep statement. I'm looking for a + or - 0 or 1 time...followed by one or more numbers...what am I doing wrong?

EDIT: It may be with my translate command sine I've never used it before. I need to delete all +'s in the (valid) argument....how do I do that?

Last edited by FuzzyNips; 11-10-2004 at 11:50 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-10-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
I tried some thing like this , which might help you ;



$ echo "-1" | egrep "^[\+\-]" | tr "-" " "
1
$
$ echo "+1" | egrep "^[\+\-]" | tr "+" " "
1
Reply With Quote
  #3 (permalink)  
Old 11-10-2004
Registered User
 

Join Date: Oct 2004
Posts: 5
Ok I got it working finally!

Code:
# Checks for valid argument
echo $1 | egrep -s "(^[\+]?[0-9]+$)|(^[-]?[0-9]+$)"

# If argument is valid then return its value
# Exits program with return code of 0
if [ $? -eq 0 ]; then
        # Removes a leading '+' sign if there is one
        echo $1 | tr -d "+" " "

        exit 0

# If argument is invalid then return an error message
# Exits program with return code of 1
else
        echo "Invalid operand: $1"

        exit 1
fi

Last edited by FuzzyNips; 11-10-2004 at 01:17 PM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0