LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2011, 10:26 PM   #1
lynne007
LQ Newbie
 
Registered: Jul 2010
Location: usa
Posts: 10

Rep: Reputation: 0
Question getopts - how to read in a regular expression without interpretation


I am modifying a script that uses a getopts to read in options. One of the options allows to user to narrow down the files searched. The user can specify a pattern like *.c* for all files of type .c and .cpp.

the script xxx is called like this:

xxx -n *.c*

in this logic:
while getopts ":l:n:d:s" opt; do
case $opt in
l ) LIB=$OPTARG ;;
d ) SEARCH_DIRS=$OPTARG ;;
n ) NAME="$OPTARG" ;;
s ) SHOW="" ;;
esac
done
if I say NAME='$OPTARG', then I get the literal '$OPTARG'. If I put "$OPTARG", in the script, when option n is saved in a variable NAME, instead of NAME being equal to '*.c*', NAME is equal to the first file with a type starting with c (*.c*) found in the directory from which I am executing xxx. It is like it is interpreting the regular expression before saving it in variable NAME.

How can I the literal '*.c*' to be stored in NAME?

thanks.
 
Old 04-25-2011, 10:35 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
you need to quote the value (note the quotes):
Code:
xxx -n '*.c*'
 
Old 04-25-2011, 11:57 PM   #3
lynne007
LQ Newbie
 
Registered: Jul 2010
Location: usa
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Berhanie View Post
you need to quote the value (note the quotes):
Code:
xxx -n '*.c*'
so.. there is no way to specify that the option is not to be interpreted? The user has to quote any regular expression that is used as input?
 
Old 04-26-2011, 12:12 AM   #4
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Quote:
there is no way to specify that the option is not to be interpreted?
no, because it's the shell that expands it before calling the script.
 
Old 04-26-2011, 12:37 AM   #5
lynne007
LQ Newbie
 
Registered: Jul 2010
Location: usa
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Berhanie View Post
no, because it's the shell that expands it before calling the script.
ok. Thanks.
 
Old 04-26-2011, 03:07 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Read up on how bash quoting and shell expansion work here:

http://tldp.org/LDP/Bash-Beginners-G...ect_01_04.html
http://tldp.org/LDP/Bash-Beginners-G...ect_03_03.html
http://tldp.org/LDP/Bash-Beginners-G...ect_03_04.html

In short, when the shell reads an input line, it first looks for all the characters and expressions on the line that have reserved shell meanings. It then processes these, expanding and substituting them with the actual contents they represent. Then finally it executes the expanded command(s).

Quoting is what you use to protect strings from this expansion process, allowing them to be passed to the command you're running as-is.


By the way, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression help smeezekitty Programming 7 03-27-2011 08:57 PM
Regular Expression lewkh Linux - General 3 04-30-2010 04:01 AM
regular expression (.*?) uttam_h Programming 6 05-30-2008 06:45 PM
Help with regular expression Feyd-Rautha Programming 8 04-21-2008 12:18 PM
Regular Expression msvinaykumar Programming 2 08-14-2006 09:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration