The option string argument you were passing to getopts would be acceptable to recent versions of the Korn shell getopts built-in. (I'm not sure when this was added to ksh, but it was not in the early versions of ksh93. I am not aware of any version of bash that accepts this form of option specification for its getopts built-in.) Note that having a : after the short option name specifies that that option takes an option argument. (Also note that the case statement processing your command line arguments does not expect option arguments for the -d, -h, and -l options; so there should not have been a colon following these letters in the first operand to getopts. And, with ksh, long option names must be presented using a double leading minus sign (e.g., --list) while short options are presented using a single leading minus sign (e.g., -l) and multiple short options (without option-arguments) can be grouped behind a single minus sign (e.g., -dl).
In general optional option-arguments are a bad idea. However, you can assign an empty string as the value of an option argument and behave appropriately if the assigned option-argument value is the empty string.
If you have a recent version of ksh, play around with script to see getopts works with short and long options:
If you save this code in a file named tester, make it executable:
Then you can see that all of the commands:
all produce exactly the same output:
while the command:
works as expected, but
will not work because with a single minus sign, only short options are allowed and there is no -i option defined, and produces the diagnostic:
To see how to specify an empty string an an option argument, try:
but note that the following will not work:
This User Gave Thanks to Don Cragun For This Post:
Hi All,
I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working.
Can someone please let me know what wrong in this code.
function1()... (4 Replies)
Hello Gurus :)
I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch.
This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help.
Specialy since... (3 Replies)
while getopts v OPTION
do
case $OPTION in
v) echo "Hello" ;;
*) exit 1;;
esac
done
Suppose I have script tmp.sh
Whose Signature is tmp.sh <fixed_argument>
When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
EDIT: -- SOLVED --
Heyas,
Getting used to optargs, but by far not understanding it.
So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals.
Passing:
browser -t test -d sect $HOME
Where -t should change the title,
and -d... (0 Replies)
How can I say one of the options is required? can I use an if statement?
let say:
while getopts ":c:u:fp" opt; do
case $opt in
c) echo "-c was triggered, Parameter: $OPTARG" >&2;;
u) echo "-u was triggered, Parameter: $OPTARG" >&2;;
f) echo "-u was triggered,... (2 Replies)
Suppose I have a code below .
while getopts a: opt
do
case $opt in
a) app_name="$OPTARG";;
*) echo "$opt is an invalid option";
exit 1;;
?) echo "The value of $OPTARG is an invalid option";
exit 1;;
esac
done
Could anyone please tell me in which case my... (1 Reply)
Hi,
Unexpectedly, the function below doesn't seem to work
when called a second time. The output shows that when
the function is called the first time, it works as expected
but when it is called a second time, the loop that processes
the options passed to the function, with the while loop,... (2 Replies)
Hi,
I have a program where I want to use getopts.
I want to use "-i" option and then optionally supply arguments.
If user dosent supply arguments, then also it should work.
Please tell me how to proceed.
Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Hi
i have part of the scripts below ,getopt for -h or ? not working for me.
can anybody tell me if this sytax right or wrong.
#!/usr/bin/ksh
program=$(basename $0)
#####################################################################################
function usageerr
{
RC=1
... (3 Replies)
hey
need help with getopts again.
i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument
e.g srcipt_name -f 3
i am able to use getopts to do this but i am having problems two accept more than two agruments
e.g.... (1 Reply)