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.
The usual order is "options first, filenames last", for a good reason.
Ok, for an exercise.
The shift command shifts out the FIRST argument not the last
The positional parameters could be copied to an array, where elements can be deleted at any position.
But you said Bourne shell? It does not have arrays
Once you go beyond one or 2 args, it's good idea to use something like getopts, as shown by teckk.
You need to ensure that even you can't get the 'order' wrong ..
.sh should be standard shell or Posix shell.
All current shells are compliant, some have more individual features (e.g. arrays).
The Bourne shell came with Unix SysV before there was a standard.
It had some bugs and design flaws. Unix Vendors started with individual fixes.
The standard has standardized the fixes, and added a few features (e.g. integer arithmetic).
I guess we need the reverse direction, so pick the last argument (also remove it) and put it in front of the others.
The question is if we need bourne shell or we can use bash here.
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.
compressor newarchive.comp *.txt 9
The OP wants to enter the compression level last for whatever reason and I assume like everyone else that the arguments have to be reordered using a script to match the required order. Since most existing compression utilities use some sort of command line argument option I would probably go with a default option similar to /etc/default.
The OP wants to enter the compression level last for whatever reason ...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.