So this is my code:
#!/bin/bash
action_list='list|add|rem'
while true; do
echo "Actions include: list - show list
add - add item to list
rem - remove item from list"
read -p "Input action: " action_var
case $action_var in
${action_list} ) echo "Option $action_var is valid";;
*) echo "Option $action_var is INVALID";;
esac
echo $action_var
done
What I want is to use the case command such that, in the future if I add more options, I don't need to hardcode them in, I can just use the "action_list" variable.
But the ${action_list} )
construct does not work.
Now I tried hardcoding it like list|add|rem)...
and it works.
Why wouldn't a variable work in this case?
[[
extended test and regular expression matching (if [[ $action_var =~ $action_list ]]; then ...
). Or one of the options in How do I test if an item is in a bash array? – muru Nov 03 '19 at 04:06$action_var=do
and$action_list="sum drop dont etc"
it would be matched, no? So I'd have to add some magic and add$action_var="^whatever_the_var_is$"
right? – user361323 Nov 03 '19 at 04:09$action_list
and the one that would match$action_var
is at the end of the list, it'd have to iterate through all of them. The other answers (associative arrays) are a bit more complex and I need to study them a bit more. – user361323 Nov 03 '19 at 04:19