![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|||
| Google UNIX.COM |