7 edited body
source | link

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quotes around multiple of arguments

    Put quotes around multiple arguments

    In this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Use comma (or other preferred character) as a delimiter

    Use comma (or other preferred character) as a delimiter

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements. As pointed out in the comments section this solution is chosen by some common programs e.g. lsblk -o NAME,FSTYPE,SIZE.

  3. Allow multiple -r options

    Allow multiple -r options

    Multiple -r, but each taking only one argument:

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain. This one is also used by some standard linux tools e.g. awk -v var1=x -v var2=y.

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quotes around multiple of arguments

    In this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Use comma (or other preferred character) as a delimiter

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements. As pointed out in the comments section this solution is chosen by some common programs e.g. lsblk -o NAME,FSTYPE,SIZE.

  3. Allow multiple -r options

    Multiple -r, but each taking only one argument:

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain. This one is also used by some standard linux tools e.g. awk -v var1=x -v var2=y.

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quotes around multiple arguments

    In this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Use comma (or other preferred character) as a delimiter

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements. As pointed out in the comments section this solution is chosen by some common programs e.g. lsblk -o NAME,FSTYPE,SIZE.

  3. Allow multiple -r options

    Multiple -r, but each taking only one argument:

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain. This one is also used by some standard linux tools e.g. awk -v var1=x -v var2=y.

6 added 192 characters in body
source | link

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quotes around multiple of arguments

    Put quote around multiple of arguments, so thatIn this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Another alternative could be to use , (or other preferred char) as a delimiter:

    Use comma (or other preferred character) as a delimiter

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements. As pointed out in the comments section this solution is chosen by some common programs e.g. lsblk -o NAME,FSTYPE,SIZE.

  3. Allow multiple -r options

    Yet another possibility is to allow multipleMultiple -r options, but each taking only one argument.:

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain. This one is also used by some standard linux tools e.g. awk -v var1=x -v var2=y.

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quote around multiple of arguments, so that getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Another alternative could be to use , (or other preferred char) as a delimiter:

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements.

  3. Yet another possibility is to allow multiple -r options, each taking only one argument.

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain.

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quotes around multiple of arguments

    In this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Use comma (or other preferred character) as a delimiter

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements. As pointed out in the comments section this solution is chosen by some common programs e.g. lsblk -o NAME,FSTYPE,SIZE.

  3. Allow multiple -r options

    Multiple -r, but each taking only one argument:

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain. This one is also used by some standard linux tools e.g. awk -v var1=x -v var2=y.

5 missing quotes, fixed incorrect usage of the split+glob operator. spurious :
source | link

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quote around multiple of arguments, so that getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr::l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG ";"
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in $"${array[@]};"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Another alternative could be to use , (or other preferred char) as a delimiter:

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the script you can split this argument and put into an array using parameter substitution mechanism:code above. That one has the advantage of allowing empty elements.

    array=(${OPTARG//,/ })
    
  3. Yet another possibility is to allow multiple -r options, each taking only one argument.

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=($OPTARG"$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain.

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quote around multiple of arguments, so that getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr::l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "; array=($OPTARG) ;;
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in ${array[@]}; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Another alternative could be to use , (or other preferred char) as a delimiter:

    ./script -r 123,456,789
    

    and in the script you can split this argument and put into an array using parameter substitution mechanism:

    array=(${OPTARG//,/ })
    
  3. Yet another possibility is to allow multiple -r options, each taking only one argument.

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=($OPTARG)
    

You cannot pass two arguments with single option using getopts.

I recommend the following alternatives:

  1. Put quote around multiple of arguments, so that getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once:

    #!/bin/bash  
    
    while getopts ":hr:l:" opt; do
        case $opt in
            r ) echo "Run Numbers - argument = $OPTARG "
                set -f # disable glob
                IFS=' ' # split on space characters
                array=($OPTARG) ;; # use the split+glob operator
            l ) echo "Latency range - argument = $OPTARG" ;;
            h ) helptext
                graceful_exit ;;
            * ) usage
                clean_up
                exit 1
        esac
    done
    
    echo "Number of arguments: ${#array[@]}"
    echo -n "Arguments are:"
    for i in "${array[@]}"; do
      echo -n " ${i},"
    done
    printf "\b \n"
    

    The example of run:

    ./script -r "123 456 789"
    

    And output:

    Run Numbers - argument = 123 456 789 
    Number of arguments: 3
    Arguments are: 123, 456, 789
    
  2. Another alternative could be to use , (or other preferred char) as a delimiter:

    ./script -r 123,456,789
    

    and you just replace IFS=" " with IFS=, in the code above. That one has the advantage of allowing empty elements.

  3. Yet another possibility is to allow multiple -r options, each taking only one argument.

    ./script -r 123 -r 456 -r 789
    

    Then arguments would be added to array one by one

    array+=("$OPTARG")
    

    That one has the advantage of not having limitations on what characters the elements may contain.

4 deleted 142 characters in body
source | link
3 added 491 characters in body
source | link
2 added 117 characters in body
source | link
1
source | link