ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221
Rep:
Need Help with getopts
Hi,
I am trying to use getopts on bash. Operating System is Mac 10.6.7
The aim is to test if an argument is provided with an option then it should take that or else if should take the default value defined in the script.
Currently if I provide the options with arguments it works but if I don't provide an argument, it takes next option as the argument for first option.
Here is the script:
$cat npc
Quote:
#!/bin/bash
user=parag
rh=redhat
while getopts "r:u:" os
do
case "$os" in
r) if [ $OPTARG ]; then oss=$OPTARG; else oss=$rh; fi ;;
u) if [ $OPTARG ]; then usr=$OPTARG; else usr=$user ; fi ;;
esac
done
echo $oss
echo $usr
Quote:
$ ./npc -r redhat -u pk
redhat
pk
Quote:
$./npc -r -u
-u
In last example can we make it print "redhat" and "parag"
Consider your logic. If someone uses "-r something", there
will always be an $OPTARG. So the test with the $OPTARG
value will always be true. If someone doesn't use the -r
option, that test will never run.
So you'd want to set your default values before reading
any input. Then if someone selects an option, assign the
new value to the variable.
...or maybe just parse args, throw an error and exit when OPTARG starts with a dash?:
Code:
[ "${OPTARG:0:1}" = "-" ] && { echo "A chair is still a chair
Even when there's no one sitting there
But a chair is not a house
And a house is not a home
(exiting)." >/dev/stderr; exit 127; }
...or maybe just parse args, throw an error and exit when OPTARG starts with a dash?:
Code:
[ "${OPTARG:0:1}" = "-" ] && { echo "A chair is still a chair
Even when there's no one sitting there
But a chair is not a house
And a house is not a home
(exiting)." >/dev/stderr; exit 127; }
No! Use carltm's advice. The point of getopt is that it will decide if there is an error on input. Otherwise, what is the point of using getopt if one must "parse" the input themselves?
+check_opts() { [ "${OPTARG:0:1}" = "-" ] && { echo "Invalid arg, exiting." >/dev/stderr; exit 127; }; }
while getopts "r:u:" os
do
case "$os" in
-r) if [ $OPTARG ]; then oss=$OPTARG; else oss=$rh; fi ;;
-u) if [ $OPTARG ]; then usr=$OPTARG; else usr=$user ; fi ;;
+r) if [ $OPTARG ]; then check_opts; oss=$OPTARG; else oss=$rh; fi ;;
+u) if [ $OPTARG ]; then check_opts; usr=$OPTARG; else usr=$user ; fi ;;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.