Multiple Parameter Parsing
suggest changeTo parse lots of parameters, the prefered way of doing this is using a while loop, a case statement, and shift.
shift
is used to pop the first parameter in the series, making what used to be $2, now be $1. This is useful for processing arguments one at a time.
#!/bin/bash # Load the user defined parameters while [[ $# > 0 ]] do case "$1" in -a|--valueA) valA="$2" shift ;; -b|--valueB) valB="$2" shift ;; --help|*) echo "Usage:" echo " --valueA \"value\"" echo " --valueB \"value\"" echo " --help" exit 1 ;; esac shift done echo "A: $valA" echo "B: $valB"
Inputs and Outputs
$ ./multipleParams.sh --help Usage: --valueA "value" --valueB "value" --help $ ./multipleParams.sh A: B: $ ./multipleParams.sh --valueB 2 A: B: 2 $ ./multipleParams.sh --valueB 2 --valueA "hello world" A: hello world B: 2
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents