The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find the last argument in a argument line? nehagupta2008 UNIX for Dummies Questions & Answers 4 06-20-2008 11:05 AM
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 12:46 PM
Argument list too long - Shell error dad5119 Shell Programming and Scripting 8 03-03-2006 03:57 PM
getopts yerra Shell Programming and Scripting 5 03-26-2005 10:43 AM
command find returned bash: /usr/bin/find: Argument list too long yacsil Shell Programming and Scripting 1 12-15-2003 06:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2008
duderonomy duderonomy is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 17
getopts and a list argument

Hi,

I'm using bash and ksh93 compatible derivatives.

In a recent getopts experience, I found myself spending far too much
time on this little problem. I hope someone can help...

So here's the deal.

I want to build have a command line interface that accepts either zero,
one, or many parameters of the same type. Assume the script sends
e-mails to a recipient list. I tried to write the code to handle this, but it
did not work:

commandname -r "user1@tld user2@tld user3@tld"

This did not work because the getopts seemed to parse strings using
space as delimiter regardless of the existence of the surrounding double
or single quotes. This resulted in OPTARGS containing only the first
element of the list. (e.g. user1@tld)

So, I changed the UI to be such that any number (0, 1, or more) of the
-r options can be on the command line.

commandname -r user1@tld -r user2@tld -r user3@tld

Now, my getopts loop has this form:

Code:
while getopts "r:" opt; do
    case $opt in
        r) ONE_STRING="${OPTARG}"
            ;;
    esac
    STRING_LIST="${STRING_LIST} ${ONE_STRING}"
done
It successfully grabs every single occurrence of the -r option from the
command line. I created the variable, STRING_LIST, to "accumulate" the
strings.

Is there a better way to do this style of zero, one, or more options of
the same type?

Is there a better way to build a list that does not create anomalies such
as a space character at the beginning or end. (My example above has a
single space character as the 1st character of STRING_LIST, due to fact
it is an empty variable the first time it is referenced on the RHS of
the = sign.)

#####

BTW, I am unable change tools or languages so if you want to suggest
that I use Perl or Ruby or even Java, then please include a example code
(for others to see) otherwise why bother acting like a smarty pants if
you can't display a nice example?
  #2 (permalink)  
Old 06-23-2008
unSpawn unSpawn is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 50
Quote:
Originally Posted by duderonomy View Post
commandname -r user1@tld -r user2@tld -r user3@tld
With Bash v2 and v3 I get:
Code:
showGetopts() { while getopts "a:b:c:" opt; do case "${opt}" in a|b|c) echo "${opt}: \"${OPTARG}\"";; esac; done; }; showGetopts -a "a@b c@d" -b e@f g@h -c 1 23 4
a: "a@b c@d"
b: "e@f"
Since quoted inconsistently parts of "-b" and "-c" is completely gone from the output.
  #3 (permalink)  
Old 06-23-2008
duderonomy duderonomy is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 17
Hmmm... I wonder what I was doing incorrectly, then?
I will investigate again and get back to this thread.

Thank you!

PS: thx for the nifty way to test options too!

Last edited by duderonomy; 06-23-2008 at 09:24 PM..
  #4 (permalink)  
Old 06-23-2008
duderonomy duderonomy is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 17
I found my bug. I was mucking up command line data before getopts
even had a chance to get a whack at it! Do'h! <Sheepishly> It was
caused by something I was not showing. Ugh.

My "main" was calling a function "process_command_line()" where this
getopts work was being performed. I was calling it this way:

process_command_line $@

I really thought I was doing it correctly by using $@ instead of $*.
Though, not entirely. Evidently, that variable needs to be double quoted,
to get the expected behavior. Like this:

process_command_line "$@"

I wonder why that is? What was I breaking?

Thanks for the feedback! I'm glad I got straightened out now so that I
could avoid writing additional (and unnecessary) code to work around
my bug.

Hmmm... This makes me wonder if %SOME_LARGE_PERCENTAGE% of
the existing code base in the world is a workaround for what the
programmer did not understand.

Cheers
  #5 (permalink)  
Old 06-24-2008
unSpawn unSpawn is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 50
Quote:
Originally Posted by duderonomy View Post
I wonder why that is? What was I breaking?
The easiest way is make one function "old" style, one "new" style, 'set -vx', then compare output.


Quote:
Originally Posted by duderonomy View Post
Hmmm... This makes me wonder if %SOME_LARGE_PERCENTAGE% of the existing code base in the world is a workaround for what the programmer did not understand.
I don't know the answer to that, but one example I can think of was KDE vs QT. In the past the KDE team did a lot of odd tweakage instead of using QT to its fullest. That changed though. Even if a programmer understands it all there's bound to be hundreds of ways to do something "better" (more efficient, more performant). Wrt scripting reading static guides like the ABS (again), reading fora like these and other peoples scripts helps, the rest is experimenting and "just" gaining experience. And as F/OSS shows, if things can be improved they will be.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0