LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-23-2023, 06:17 AM   #1
JASlinux
Member
 
Registered: Oct 2020
Posts: 387

Rep: Reputation: Disabled
Question How to reorder command line arguments in a script?


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
[maximum compression]

How do I assign those variables in the correct order in Bourne?
 
Old 02-23-2023, 06:22 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,283

Rep: Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423
see the commands shift, the variable $# and here: https://opensource.com/article/18/5/...ro-bash-arrays (for example)
 
Old 02-23-2023, 09:42 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,171
Blog Entries: 6

Rep: Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847Reputation: 1847
Bash example:
Code:
#!/usr/bin/bash

#Help
Help() {
    echo "
    Syntax: "$0" <file>
    Optional: "$0" [ -a -c -f -h ]

    Options:
    -a archive name
    -c Compression level: Options are 1 2 3 4
    -f Input files, \"Quote multiple file names\"
    -h This help menu
    "
}

#Get opts from switches
while getopts a:c:f:h option; do
    case "${option}" in
        a)  archive=${OPTARG} ;;
        c)  compression=${OPTARG} ;;
        f)  files=${OPTARG} ;;
        h)  Help; exit ;;
        *)  echo "Unknown switch"; exit ;;
    esac
done

echo "You entered: "$0" -a "$archive" -f "$files" -c "$compression""
Code:
./getopts.sh -a foo -f "one two three" -c 2
You entered: getopts.sh -a foo -f one two three -c 2
 
Old 02-23-2023, 10:59 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,875

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
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

A brain teaser.
 
Old 02-23-2023, 11:42 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,875

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
Found a solution, works in all Posix-compatible shells.

Code:
#!/bin/sh
n=$#
while [ $((n-=1)) -gt 0 ]
do
  set -- "$@" "$1"
  shift
done
echo "args":
printf "%s\n" "$@"
echo
echo compressor "$@"
 
Old 02-23-2023, 10:00 PM   #6
JASlinux
Member
 
Registered: Oct 2020
Posts: 387

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
But you said Bourne shell? It does not have arrays
.sh are Bourne, no?

Code:
#!/bin/sh
is same so I believe it is a standard script.

I will experiment & report results. It appears more complicated than I expected.

I realized even if we assign variables names (=), a script will place them in the order given on the command line without manipulation.
 
Old 02-23-2023, 11:05 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,378

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
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 ..
 
Old 02-24-2023, 01:48 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,875

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
.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).
 
Old 02-24-2023, 02:24 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,725

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by MadeInGermany View Post
Found a solution, works in all Posix-compatible shells.

Code:
#!/bin/sh
n=$#
while [ $((n-=1)) -gt 0 ]
do
  set -- "$@" "$1"
  shift
done
echo "args":
printf "%s\n" "$@"
echo
echo compressor "$@"
Does the following not work?
Code:
#!/bin/sh
c="$1"
shift
echo compressor "$@" "$c"
Evo.
 
1 members found this post helpful.
Old 02-24-2023, 03:09 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,283

Rep: Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423Reputation: 7423
Quote:
Originally Posted by evo2 View Post
Does the following not work?
Code:
#!/bin/sh
c="$1"
shift
echo compressor "$@" "$c"
Evo.
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.
 
Old 02-24-2023, 03:11 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,875

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
@evo2 That puts the first argument last.
I think the goal is to put the last argument first.

Last edited by MadeInGermany; 02-24-2023 at 03:14 AM.
 
Old 02-24-2023, 05:53 PM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,725

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by MadeInGermany View Post
@evo2 That puts the first argument last.
I think the goal is to put the last argument first.
Really? OP posted:

Quote:
Originally Posted by JASlinux View Post
On the command line I want to enter the compression level LAST.
Evo2.
 
Old 02-24-2023, 05:56 PM   #13
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,725

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by pan64 View Post
I guess we need the reverse direction, so pick the last argument (also remove it) and put it in front of the others.
Really? OP posted:

Quote:
Originally Posted by JASlinux View Post
syntax: compressor [compression level] [archive name] [files to add]

On the command line I want to enter the compression level LAST.
Evo2.
 
Old 02-24-2023, 06:19 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,928

Rep: Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001Reputation: 6001
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.

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.
 
Old 02-24-2023, 06:40 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,286
Blog Entries: 24

Rep: Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238Reputation: 4238
Quote:
Originally Posted by michaelk View Post
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.
 
  


Reply

Tags
bourne, scripting, scripts, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:31 PM.

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