Skip to main content
added 71 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

For your command line parsing, arrange with the pathname operands to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

a_opt=false b_opt=false c_opt=false
while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()
  a_opt=false b_opt=false c_opt=false
while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname operands to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()
 
while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname operands to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

a_opt=false b_opt=false c_opt=false
while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=() a_opt=false b_opt=false c_opt=false
while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2
Typo.
Source Link
user232326
user232326

For your command line parsing, arrange with the pathname opernadsoperands to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname opernads to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname operands to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2
added 1 character in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

For your command line parsing, arrange with the pathname opernads to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname operandargument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname opernads to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname operand "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2

For your command line parsing, arrange with the pathname opernads to always be the last ones on the command line:

./myscript -a -b -c -- 'foo bar' 'another file' file[12]

The parsing of the options would look something like

while getopts abc opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname do
    # process pathname operand "$pathname" here
done

The shift will make sure to shift off the handled options so that the pathname operands are the only things left in the list of positional parameters.

If that's not possible, allow the -i option to be specified multiple times and collect the given arguments in an array each time you come across it in the loop:

pathnames=()

while getopts abci: opt; do
     case $opt in
         a) a_opt=true ;;
         b) b_opt=true ;;
         c) c_opt=true ;;
         i) pathnames+=( "$OPTARG" ) ;;
         *) echo error >&2; exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

for pathname in "${pathnames[@]}"; do
    # process pathname argument "$pathname" here
done

This would be called as

./myscript -a -b -c -i 'foo bar' -i 'another file' -i file1 -i file2
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading