![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| If statement - How to write a null statement | april | Shell Programming and Scripting | 3 | 04-16-2008 02:14 PM |
| How to match the last XML extension by using Case statement | sunitachoudhury | Shell Programming and Scripting | 3 | 04-06-2008 02:19 AM |
| if statement to match * | sivasenthil_k | UNIX for Dummies Questions & Answers | 3 | 09-20-2006 12:39 PM |
| How to concatenate two strings or several strings into one string in B-shell? | fontana | Shell Programming and Scripting | 2 | 08-26-2005 12:58 PM |
| sed regex | Shakey21 | UNIX for Dummies Questions & Answers | 4 | 01-31-2002 09:16 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Regex in if-then-else statement to match strings
hello I want to do a pattern match for string in the if statement, but I am not sure how to use regex inside the if statement. I am looking for something like this: Code:
if [ $name = [ab]{2,3} ]; then
.....
....
...
fi
|
|
|||||
|
Hi. Quote:
Code:
#!/bin/bash3
# @(#) s4 Demonstrate case selectors.
set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
echo " MUST USE VERSION 3 FOR REGULAR EXPRESSIONS WITH =~ OPERATOR!"
# See: http://www.unix.com/showthread.php?p=302136557&posted=1#post302136557
echo
# if [ $name = [ab]{2,3} ]; then
name="b"
name="bb"
echo " Original string = \"$name\""
case $name in
[ab]{2,3} )
echo Success
;;
* )
echo Failure
;;
esac
echo
name="b{2,3}"
echo " Original string = \"$name\""
case $name in
[ab]{2,3} )
echo Success
;;
* )
echo Failure
;;
esac
exit 0
producing: Code:
% ./s4
GNU bash 3.00.16(1)-release
MUST USE VERSION 3 FOR REGULAR EXPRESSIONS WITH =~ OPERATOR!
Original string = "bb"
Failure
Original string = "b{2,3}"
Success
... cheers, drl |
|
|||||
|
Yes, it's globbing, not re's. For {2,3} with globbing: Code:
bash 3.2.25(1)$ v=b bash 3.2.25(1)$ case $v in ([ab][ab]|[ab][ab][ab]) echo OK;;(?) echo KO;;esac KO bash 3.2.25(1)$ v=bbb bash 3.2.25(1)$ case $v in ([ab][ab]|[ab][ab][ab]) echo OK;;(?) echo KO;;esac OK bash 3.2.25(1)$ # or: bash 3.2.25(1)$ shopt -s extglob bash 3.2.25(1)$ v=b bash 3.2.25(1)$ case $v in ([ab][ab]?([ab])) echo OK;;(?) echo KO;;esac KO bash 3.2.25(1)$ v=bb bash 3.2.25(1)$ case $v in ([ab][ab]?([ab])) echo OK;;(?) echo KO;;esac OK |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|