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:
None of the examples I've found show how to use the optional ARGS... variable length argument list. All examples focus on the positional arguments, which I know how to use.
I've tried several tests, but, the only effect of adding ARGS... to my script causes the positional arguments to be ignored ... nothing I've tried does anything with the optional arguments.
So, I'm curious ... can anyone give me a short example of how a variable length argument lists specified by ARGS... is used?
Thanks.
Also, please don't ask me to provide everything I've tried. They all fail, and, I'd rather cut to the chase with one simple example of what works.
... the only effect of adding ARGS... to my script causes the positional arguments to be ignored
Which is as specified.
Quote:
... nothing I've tried does anything with the optional arguments.
Strange - appending a "line" of options should (and DOES!) make getopts evaluate those in lieu of the positional parameters, and it does for me. Pls (I know you dislike this) show an example of your failed attempts.
EDIT: Are you sure you did differentiate between the bash builtin getopts and the external command getopt?
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here are the guts of a test script, "s0", which calls helper scripts "s1" and "s2", using getopt and getopts, adapted from A small example on Bash getopts. #bash #getopt #getopts . GitHub, which in turn, was one of the top results of a Google search for keywords short example of getopt and getopts:
producing:
You can then test these yourself as required.
Hi Unix Gurus,
i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code:
function... (3 Replies)
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)
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)
I'm using getopts to process command line args in a Bash script. The code looks like this:
while getopts ":cfmvhs:t:" option; do
case $option in
c) operationMode="CHECK"
;;
f) operationMode="FAST"
;;
m) ... (6 Replies)
In the below code
while getopts :rfw:d:s:a: options
do
case "$options" in
r) echo reverse;;
f) echo forward;;
w) window=$OPTARG;;
d) duration=$OPTARG;;
s) search=$OPTARG;;
a) value=$OPTARG;;
*) help; exit;;
esac
done
... (2 Replies)
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)
Is it possible to have a non-const reference variable as an OPTIONAL/DEFAULT parameter to c++ function
ex
void read(string &data,int &type=0 /*or something*/) ;
so i will call
read(data);
//or
int type;
read(data,type);
printf("Type =%d",type);
I found one dirty workaround
... (2 Replies)
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)
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... (4 Replies)
Hi, I use getopts in this way:while getopts ":d:f:crapv" Option
do
case $Option in
d ) BACKUP_DIR="$OPTARG";echo $BACKUP_DIR;; #echo fot test
c ) compress_file;;
r ) remove_file;;
a ) remove_file && compress_file;;... (2 Replies)