I want to write a function that checks if a given variable, say, var
, starts with any of the words in a given list of strings. This list won't change.
To instantiate, let's pretend that I want to check if var
starts with aa
, abc
or 3@3
.
Moreover, I want to check if var
contains the character >
.
Let's say this function is called check_func
. My intended usage looks something like
if check_func "$var"; then
do stuff
fi
For example, it should "do stuff" for
aardvark
, abcdef
, 3@3com.com
and 12>5
.
I've seen this SO question where a user provides part of the work:
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
My idea is that I would iterate over the list mentioned above and use this function. My difficulty lies in not understanding exactly how exiting (or whatever replaces returning) should be done to make this work.
Best Answer
I divided the tests up into two functions. Both use
case ... esac
and returns success (zero) as soon as this can be determined. If nothing matches, failure (1) is returned.To make the list of prefixes more of a dynamic list, one could possibly write the first function as
(the value to inspect is the first argument, which we save in
value
and thenshift
off the list of arguments to the function; we then iterate over the remaining arguments) and then call it asThe second function could be changed in a similar manner, into
(to check for some arbitrary substring), or
(to check for any of a number of substrings)