Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
02-24-2023, 07:31 PM
|
#16
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Maybe I'm misinterpreting the question but my take is that there is some existing program called "compressor". It expects arguments as follows:
Code:
% compressor --help
USAGE: compressor <compression level> <inputfiles> <outputfile>
And OP wants a wrapper script, such as:
Code:
% compressor.sh --help
USAGE: compressor.sh <inputfiles> <outputfile> <compression level>
My post was an example of compressor.sh
Evo2.
|
|
|
02-24-2023, 07:39 PM
|
#17
|
Moderator
Registered: Aug 2002
Posts: 26,130
|
Ok, so we all have the same understanding.
|
|
|
02-25-2023, 01:24 AM
|
#18
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
Quote:
Originally Posted by astrogeek
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.
|
|
|
06-12-2023, 05:45 PM
|
#19
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
Quote:
Originally Posted by JASlinux
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.
|
|
|
06-13-2023, 03:48 AM
|
#20
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,918
|
I gave the solution. (But evo2 must have misunderstood something, and the OP did not come back.)
Code:
% cat compressor.sh
#!/bin/sh
n=$#
while [ $((n-=1)) -gt 0 ]
do
set -- "$@" "$1"
shift
done
echo compressor "$@"
% chmod 755 compressor.sh
% touch a.txt b.txt
% ./compressor.sh newarchive.comp *.txt 9
compressor 9 newarchive.comp a.txt b.txt
This solves the requirement. Of course the echo is for demonstration and must be removed to really run the "compressor".
Quote:
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:
compressor newarchive.comp *.txt 9
|
|
|
|
06-15-2023, 12:37 AM
|
#21
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by MadeInGermany
I gave the solution. (But evo2 must have misunderstood something, and the OP did not come back.)
|
No. There are two different interpretations as to what was being asked. OP never clarified.
|
|
|
06-15-2023, 02:43 AM
|
#22
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,925
|
Also '9' can be a perfectly valid filename.
|
|
|
06-15-2023, 06:51 PM
|
#23
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,325
|
Just make the change to "compressor" itself. It's open-source, isn't it?
Last edited by dugan; 06-15-2023 at 06:52 PM.
|
|
|
06-21-2023, 05:30 AM
|
#24
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515
|
Exactly astrogeek, why?
it seems like a waste of effort
|
|
|
All times are GMT -5. The time now is 03:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|