]> git.ipfire.org Git - thirdparty/u-boot.git/commit
cmd: test: add support for =~ operator
authorRasmus Villemoes <ravi@prevas.dk>
Tue, 13 May 2025 08:40:23 +0000 (10:40 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 29 May 2025 14:25:17 +0000 (08:25 -0600)
commit04044a567b6b177eb24944343d9e9e61a89edd53
treeeb359e7788bdd78d2df4dade5112de3ef8de5c7e
parent1b5e435102aa29a665119430196cb366ce36a01b
cmd: test: add support for =~ operator

Currently, the only way to make use of regex matching in the shell is
by using "setexpr [g]sub" command. That's rather awkward for asking
whether a string matches a regex. At the very least, it requires
providing setexpr with a dummy target variable, but also, the return
value of setexpr doesn't say whether any substitutions were done, so
one would have to do some roundabout thing like

  env set dummy "${string_to_test}"
  setexpr sub dummy '<some regex>' ''
  if test "${dummy}" != "${string_to_test}" ; then ...

When CONFIG_REGEX is set, teach the test command a new operator, =~,
which will allow one to more naturally write

  if test "${string_to_test}" =~ '<some regex>' ; then ...

The =~ operator with similar functionality is also supported in bash
when using its "extended" test operator [[ ]].

Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
cmd/test.c