Sponsored Content
Full Discussion: getopts and a list argument
Top Forums Shell Programming and Scripting getopts and a list argument Post 302208377 by duderonomy on Monday 23rd of June 2008 05:35:31 PM
Old 06-23-2008
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? Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

2. Shell Programming and Scripting

getopts ... only allow a single instance of an argument?

Hi there, if i have a simple getopts like below ...how can i make it so that if somebody enters more than one -g argument for example, it will error with a " you cannot enter more than one -g" or something like that.? I want to only allow one instance of a -g or a -h etc .. while getopts... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

3. Shell Programming and Scripting

getopts : two values to a single argument ?

Hi Gurus I am trying to figure out (with not much success) how to pass two values to a single getopts argument ... for example ./script -a Tuesday sausagesThe $OPTARG variable seems to only get populated with the first argument. What im looking to do is to process the first argument (i.e.make... (6 Replies)
Discussion started by: rethink
6 Replies

4. Shell Programming and Scripting

Argument list too long!!

Dear Experts, I have a list of 10K files in a directory. I am not able to execute any commands lile ls -lrt, awk, sed, mv, etc........ I wanna execute below command and get the output. How can I achieve it?? Pls help. root# awk -F'|' '$1 == 1' file_20120710* | wc -l /bin/awk: Argument list... (2 Replies)
Discussion started by: Naga06
2 Replies

5. Shell Programming and Scripting

mv : Argument list too long

Hi I am using find command -- find "directory1" -type f | xargs -i mv {} "directory2" to avoid above argument list too long problem. But, issue i am facing is directory1 is having subdirectories due to this i am facing directory traversal problem as i dont want to traverse subdirectories... (9 Replies)
Discussion started by: VSom007
9 Replies

6. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "email@xyz.com" script.ksh -m 'Message from xyz' -e 'email@xyz.com' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies

7. Shell Programming and Scripting

Argument list too long

Hi Team, Here's the situation. I have approximately 300000 to 500000 jpg files in /appl/abcd/work_dir mv /appl/abcd/work_dir /appl/abcd/process_dir The above move command will work if the jpg files count is close to 50000 (not sure). If the count is less this mv command holds good. But if... (14 Replies)
Discussion started by: kmanivan82
14 Replies

8. Shell Programming and Scripting

Specifying a list name as argument and using that list in script.

Is there a way I can specify the name of a list as an argument to a shell script and then use the values of that list name in the script? I need to do this WITHOUT using case statements. Something like this: check.sh list1 #!/bin/bash list1="www.amazon.com www.google.com"... (9 Replies)
Discussion started by: gctaylor
9 Replies

9. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

10. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies
tld_check_4tz(3)						      libidn							  tld_check_4tz(3)

NAME
tld_check_4tz - API function SYNOPSIS
#include <tld.h> int tld_check_4tz(const uint32_t * in, size_t * errpos, const Tld_table * tld); ARGUMENTS
const uint32_t * in Zero terminated array of unicode code points to process. size_t * errpos Position of offending character is returned here. const Tld_table * tld A Tld_table data structure representing the restrictions for which the input should be tested. DESCRIPTION
Test each of the code points in in for whether or not they are allowed by the data structure in tld, return the position of the first char- acter for which this is not the case in errpos. RETURN VALUE
Returns the Tld_rc value TLD_SUCCESS if all code points are valid or when tld is null, TLD_INVALID if a character is not allowed, or addi- tional error codes on general failure conditions. REPORTING BUGS
Report bugs to <bug-libidn@gnu.org>. GNU Libidn home page: http://www.gnu.org/software/libidn/ General help using GNU software: http://www.gnu.org/gethelp/ COPYRIGHT
Copyright (C) 2002-2013 Simon Josefsson. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. SEE ALSO
The full documentation for libidn is maintained as a Texinfo manual. If the info and libidn programs are properly installed at your site, the command info libidn should give you access to the complete manual. libidn 1.28 tld_check_4tz(3)
All times are GMT -4. The time now is 07:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy