Post History
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 ...
#2: Post edited
- 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
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.