On Tue, Dec 24, 2024 at 10:37:29 -0500, Roberto C. Sánchez wrote:
> I think that '==' is the wrong tool. That is testing for string
> equality, whilst you are looking for a partial match. This is what I was
> able to get working after hacking on it for a minute or two:
> 
> #! /bin/bash -u
> set -x
> BAD=0;
> while  read L;
> do if [[ $L =~ \[(U_|_U|__)\] ]]; then BAD=1; break; fi;
> done < /proc/mdstat;
> echo $BAD

Yeah, that works too.  But == or = also works here, if you use an
extended glob.  (And extended glob matching inside the [[ command is
on by default in all recent bash versions.)

= or == is a glob (or extglob) match first and foremost.  It
degenerates to a plain string match if all the characters on the
right hand side are quoted.

hobbit:~$ stat=$'blah blah\ncheese [2/1] [U_]\nblah blah'
hobbit:~$ if [[ $stat = *\[@(U_|_U|__)\]* ]]; then echo "bad"; fi
bad
hobbit:~$ stat=$'blah blah\ncheese [2/1] [UU]\nblah blah'
hobbit:~$ if [[ $stat = *\[@(U_|_U|__)\]* ]]; then echo "bad"; fi
hobbit:~$ 

Reply via email to