Results 1 to 3 of 3
Hi, referring to this syntax of case:
case word in
[(]pattern1) compound-list;;
esac
when pattern1 is a regular expression with expressions grouped using parentheses like in:
(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))\{3\}
an error occours, ...
- 07-18-2006 #1Just Joined!
- Join Date
- May 2004
- Posts
- 2
Bash scripting, use regex as a case pattern
Hi, referring to this syntax of case:
case word in
[(]pattern1) compound-list;;
esac
when pattern1 is a regular expression with expressions grouped using parentheses like in:
(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))\{3\}
an error occours, probably due to the ')' after the regular expression.
-bash: ./badscript: line 120: syntax error near unexpected token `25[0-5]'
-bash: ./badscript: line 120: ` (25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))\{3\} )'
How fix it?
Thanks
- 07-18-2006 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,065
Hi, paolodina.
I have no immediate solution for you without some thought, but the expressions used in the case statement are essentially pathname expressions (such as you use on the command line), not regular expressions:
Code:case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for path- name expansion (see Pathname Expansion below). When a match is found, the corresponding list is executed. After the first match, no subsequent matches are attempted. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list -- man bash
You may need to do some processing with sed if you want to involve regular expressions in a case statement.
Perhaps our colleagues have some more creative ideas ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 05-10-2009 #3Just Joined!
- Join Date
- Jan 2007
- Posts
- 1
case $name in
[ab]{2,3} )
echo do stuff
;;
* )
;;
esac



