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.
Sorry if I didn't make myself clear enough, what I would like to do is to give several options where options a, b, c, d, e, f are all different options. In case of the order of the options:
sh myscript -abcdef
will have the same output as
-sh myscript -fedcba
And last; think I didn't really understand your reply zeropash: could you please try to reformulate it?
The order does not matter, from what you say, I think your options don't take arguments...
Try :
Code:
#!/bin/bash
while getopts abcde option; do
case $option in
a)
echo "option: -a is set"
;;
b)
echo "option: -b is set"
;;
c)
echo "option: -c is set"
;;
d)
echo "option: -d is set"
;;
e)
echo "option: -e is set"
;;
esac
done
And execute the script with any options order you want
./script -bdeac
./script -abcde
./script -cdeba
...
Just to throw my hat in the arena, if a flag (-a, -x, -y) requires an argument, follow it with a colon in your while loop. Example, out of -abcxyz flags, pretend '-b' and '-y' require an argument (-b foo; -y foo, etc..). The argument will be stored in $OPTARG (I believe for only one loop iteration). If an argument is recieved thats not in your argument list, your variable (in this case - $arguments) will recieve a question mark in it (and you can throw an error message from there.
Code:
while getopts ab:cxy:z arguments 2>/dev/null
do
case $arguments in
a) echo "-a set!";;
b) echo "-b set with value $OPTARG";;
c) echo "-c set!";;
x) echo "-x set!";;
y) echo "-y set with value $OPTARG";;
z) echo "-z set!";;
\?) echo "Usage: scriptname [ -a ] [ -b arg ] [ -c ] etc..."
esac
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.