LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-30-2006, 08:48 AM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
bourne shell pattern matching or regular expression


Is there a way to improve the following bourne shell programming?
I wonder whether there is any pattern matching or regular expression capability or function or library for bourne shell?

#! /bin/sh
# Generated automatically by configure.

for ac_option
do
case "$ac_option" in
-version | --version | --versio | --versi | --vers | --ver | --ve | --v)
echo "./config.status generated by autoconf version 2.13"
exit 0 ;;
-help | --help | --hel | --he | --h)
echo "$ac_cs_usage"; exit 0 ;;
*) echo "$ac_cs_usage"; exit 1 ;;
esac
done
 
Old 06-30-2006, 10:10 AM   #2
binary_y2k2
Member
 
Registered: Jul 2005
Location: England, UK
Distribution: Ubuntu 8.04 Server, Kubuntu 8.04, Kubuntu 8.10, Kubuntu 9.04, Kubuntu Karmic
Posts: 698
Blog Entries: 1

Rep: Reputation: 31
1st you posted this in the wrong sub-forum, it would be better if you had posted it in "Linux-General" but never mind.
about your question:
why dont you use grep
eg:
Code:
if echo "$ac_option"|grep "\--v">/dev/null ; then
echo "./config.status generated by autoconf version 2.13"
exit 0
elif echo "$ac_option"|grep "\--h" >/dev/null ; then
echo "$ac_cs_usage"
exit 0
else echo "$ac_cs_usage"
exit 1
fi
 
Old 06-30-2006, 10:27 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 39
You can make this code a little smaller using the extended globbing features available in Bash. To enable these features you need to first set the extglob option via shopt. (Unfortunately, this is not possible under Bourne shell, so if strict Bourne compatibility is what you're aiming for then this won't cut it.) The ?() glob operator will match when it's contents (inside the parens) are found either zero or one time. Here's a very simple example:
Code:
#!/usr/bin/env bash

shopt -s extglob

case $1 in
    -?(-)v?(e?(r?(s?(i?(o?(n)))))) ) echo "version" ;;
    * ) echo "not a version" ;;
esac
Since the ?() glob operator can be nested, you can achieve the desired result, although somewhat at the cost of code readability.

As an aside, this seems to me like a fairly non-standard way of doing options. Usually you only provide two mechanisms for setting an option, short (like -v) and long (like --version). But in any case this is the best solution I can come up with to do what you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
regular expression matching linuxmandrake Programming 2 03-16-2006 06:00 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM


All times are GMT -5. The time now is 11:20 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration