while getopts abp: optchar; do
case "${optchar}" in
a) # an option
export OPTION_A=1
;;
b) # Yet another option
export b=1
;;
p) # An option to supply player names to the function
IFS=',' read -r -a PLAYER_NAMES <<<"${OPTARG}"
;;
esac
done
for name in "${PLAYER_NAMES[@]}"; do
echo ${name}
Now if you supply the 'players' keyword argument with a comma separated list, the function will ignore arguments that are separated by a space. For example if you run:
bash testexp.sh -p sherman,wilson, taylor
You will get
sherman
wilson
But taylor will not be included because his name wasn't added to the array variable.
How do you make the cli parser ignore the extra space, so that output is as follows?
sherman
wilson
taylor
#!/bin/bashor#!/usr/bin/env bash, note that it must be the first line in the script), making the script executable (chmod +x testexp.sh), and then running it directly without thebashcommand (./testexp.sh -p ...).