79756058

Date: 2025-09-04 17:54:53
Score: 1.5
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @oguzismail
  • User mentioned (0): @EdMorton
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: NateT