Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

86%
+11 −0
Q&A In a bash shell script, how to filter the command line argument list to unique entries only, for processing each?

I have a handful of shell scripts that accept any number of command line arguments, then do some relatively expensive processing based on each command line argument in turn. The general format for ...

3 answers  ·  posted 3y ago by Canina‭  ·  last activity 3y ago by Quasímodo‭

#2: Post edited by user avatar Canina‭ · 2021-09-28T13:58:46Z (about 3 years ago)
  • I have a handful of shell scripts that accept any number of command line arguments, then do some relatively expensive processing based on each command line argument in turn. The general format for these goes along the lines of
  • #!/bin/bash
  • # preliminary set-up goes here
  • # main loop:
  • while test -n "$1"
  • do
  • do_expensive_processing_for "$1"
  • shift
  • done
  • # tear-down goes here
  • This works mostly well. However, it has the downside that if I for some reason pass the same argument twice during an invocation, that argument gets processed twice. Since the processing is expensive, I want to avoid that.
  • How can I ensure that each command line argument is processed only once during a single invocation of the script, while still allowing arbitrary command line argument contents (or at least not restricting them more than the above type of bash script already would)?
  • To the extent that it matters, I'm using GNU bash 5.1.
  • I have a handful of shell scripts that accept any number of command line arguments, then do some relatively expensive processing based on each command line argument in turn. The general format for these goes along the lines of
  • #!/bin/bash
  • # preliminary set-up goes here
  • # main loop:
  • while test -n "$1"
  • do
  • do_expensive_processing_for "$1"
  • shift
  • done
  • # tear-down goes here
  • This works mostly well. However, it has the downside that if I for some reason pass the same argument twice during an invocation, that argument gets processed twice. Since the processing is expensive, I want to avoid that.
  • How can I ensure that each command line argument is processed only once during a single invocation of the script, while still allowing arbitrary command line argument contents (or at least not restricting them more than the above type of bash script already would)?
  • I'm happy with any one instance being the one that gets processed; the order of processing is not important.
  • To the extent that it matters, I'm using GNU bash 5.1.
#1: Initial revision by user avatar Canina‭ · 2021-09-28T13:56:43Z (about 3 years ago)
In a bash shell script, how to filter the command line argument list to unique entries only, for processing each?
I have a handful of shell scripts that accept any number of command line arguments, then do some relatively expensive processing based on each command line argument in turn. The general format for these goes along the lines of

    #!/bin/bash

    # preliminary set-up goes here

    # main loop:
    while test -n "$1"
    do
        do_expensive_processing_for "$1"
        shift
    done

    # tear-down goes here

This works mostly well. However, it has the downside that if I for some reason pass the same argument twice during an invocation, that argument gets processed twice. Since the processing is expensive, I want to avoid that.

How can I ensure that each command line argument is processed only once during a single invocation of the script, while still allowing arbitrary command line argument contents (or at least not restricting them more than the above type of bash script already would)?

To the extent that it matters, I'm using GNU bash 5.1.