LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-24-2023, 07:31 PM   #16
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705

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.
 
Old 02-24-2023, 07:39 PM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Ok, so we all have the same understanding.
 
Old 02-25-2023, 01:24 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 06-12-2023, 05:45 PM   #19
goumba
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
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by JASlinux View Post
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.
 
Old 06-13-2023, 03:48 AM   #20
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
Old 06-15-2023, 12:37 AM   #21
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 06-15-2023, 02:43 AM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Also '9' can be a perfectly valid filename.
 
Old 06-15-2023, 06:51 PM   #23
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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.
 
Old 06-21-2023, 05:30 AM   #24
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Exactly astrogeek, why?

it seems like a waste of effort
 
  


Reply

Tags
bourne, scripting, scripts, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell script to reorder kernel module order when booting.- need help in writing a scr deepclutch Programming 6 08-26-2014 05:14 AM
[SOLVED] script to reorder data in a row rebelbuttmunch Programming 19 05-01-2014 11:36 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
reorder partition lables Rojahon Linux - Hardware 2 07-09-2005 06:29 PM
Simple sh script: rand reorder Sinope Programming 2 09-11-2004 05:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration