as above.
basically I want to implement something like
if not match then
do these things
else
do these other things
fi
Thanks
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityas above.
basically I want to implement something like
if not match then
do these things
else
do these other things
fi
Thanks
It depends on what you mean by match, but if you mean "exactly match", you can use the string match
builtin with a plain argument.
if not string match --quiet -- "some_string" $some_argument
echo no match
else
echo match
end
To match within a string, you can use a glob in some_string
, or a regular expression with string match --regex
.
As an alternative to not string match -q -- $pattern $string
(as mentioned by @Zanchey), you can also do:
if string match -vq -- $pattern $string
echo no match
else
echo match
end
(-v
like in grep
, or --invert
(like GNU grep
's --invert-match
) to invert the match).
You'll see a difference when matching the pattern against more than one string (like when $string
above is a list with more than one element) or when matching against no string at all ($string
is an empty list).
if not string match -q -- $pattern $string1 $string2
echo none of the strings matched
else
echo at least one the strings matched
end
if string match -vq -- $pattern $string1 $string2
echo at least one of the strings did not match
else
echo all the strings matched
end
do these
blocks ?