How to reorder command line arguments in a script?
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.
I have stayed out of this discussion waiting for someone to ask what that reason actually is, so this looks like a good point to ask it myself...
@OP: What, exactly, are you trying to accomplish by reordering those args? Please describe the intended use, including a description of the problem to be solved and how you think this will solve it.
I don't think there is a problem here. It is only about the order of the arguments. I think OP just wanted a different order, which is much more convenient for him/her. I totally agree with it, there is no logic behind that, so (in general) the expected order of the arguments (in most cases) are just strange, unusual. But actually this is how it was implemented. And you always have the possibility to add a wrapper to it.
I know we have conventions, we have similar programs, just that won't make it more convenient.
Imagine a file compression utility called compressor.
syntax: compressor [compression level] [archive name] [files to add]
On the command line I want to enter the compression level LAST.
example:
Code:
compressor newarchive.comp *.txt 9
How do I assign those variables in the correct order in Bourne?
Quote:
Originally Posted by astrogeek
@OP: What, exactly, are you trying to accomplish by reordering those args? Please describe the intended use, including a description of the problem to be solved and how you think this will solve it.
The question seems to me to be: "The scripts wants the arguments in a specific order. If the user enters them in the wrong order (in the example, compression level last instead of filename), how can I store the correct value in the associated variable?"
So, it seems the OP wants to be able to take the arguments in any order, but store the values properly. Simple loops and shift won't work here.
If I'm correct in my interpretation, OP, the only solution is using getopts as posted previously (or a homebrewed argument parser). It forces the user to specify which argument is which, and can do so in whatever order. You then handle it appropriately.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.