help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: case $var in $list) issue


From: Zachary Santer
Subject: Re: case $var in $list) issue
Date: Fri, 1 Nov 2024 11:20:31 -0400

On Thu, Oct 31, 2024 at 9:55 PM #!microsuxx <fxmbsw7@gmail.com> wrote:
>
> ~ $ echo $short
> li|la|ds|fs|ac|U|S|u|s|r|i|f|c

declare -a short=( li la ds fs ac U S u s r i f c )
match='false'
for pattern in "${short[@]}"; do
  if [[ ${whatever} == ${pattern} ]]; then
    match='true'
    break
  fi
done

Probably the way to go if your list of patterns is determined dynamically.

If the case construct didn't require a | between different patterns in
the same pattern list, then you could just expand ${short[@]} there.

array=( zero one two three four )
case one in
( "${array[@]}" )
  printf 'nope one\n'
;;&
( "${array[*]}" )
  printf 'nope two\n'
;;&
( ${array[@]} )
  printf 'nope three\n'
;;&
( ${array[*]} )
  printf 'nope four\n'
;;
esac

Not sure what bash is trying to do with the quoted "${array[@]}" expansion here:

case 'zero one two three four' in
( "${array[@]}" )
  printf 'nope five\n'
;;&
( "${array[*]}" )
  printf 'yep six\n'
;;&
( ${array[@]} )
  printf 'yep seven\n'
;;&
( ${array[*]} )
  printf 'yep eight\n'
;;&
esac

yep six
yep seven
yep eight

Using [[  ]] in a for loop is going to be easier to wrap your head
around, especially if you've got whitespace and special pattern
matching characters - *, ?, [...] - which you want to do their
pattern-matching thing.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]