I ended up using sed
, shopt -s extglob
and a new variable. It was the easiest way to get what I desired without modifying the script too much:
fruits="apple orange pear banana cherry kiwi"
fruits_pattern=$(echo "$fruits" | sed 's/[[:space:]]//g')
shopt -s extglob
if ! [ "$1" = "" ]; then fruits=$*; fi
for fruit in $fruits; do
case "$fruit" in
apple)
action_1 ;;
cherry)
action_2 ;;
$fruits_pattern)
default_action ;;
*)
echo "Invalid fruit"
exit 1 ;;
esac
done
Thanks to @oguzismail for continue 2
. I did not know about it... VERY useful in the future.
Thanks to @EdMorton for a better future method using bash arrays.