Reputation: 113
I have a two variables (PACKAGE_NAME
and PACKAGE_LIST
) and I am trying to check if PACKAGE_LIST
contains PACKAGE_NAME
.
Here is what I have:
PACKAGE_LIST="rpm-foo-bar:5.0.1"
PACKAGE_NAME="rpm-foo"
if [[ ${PACKAGE_LIST} =~ "${PACKAGE_NAME}" ]]; then
echo "PACKAGE_NAME is in PACKAGE_LIST"
else
echo "PACKAGE_NAME is NOT in PACKAGE_LIST"
fi
The problem I'm having is that while rpm-foo
is similar to rpm-foo-bar
, it is not the same and should fall into the else statement (PACKAGE_NAME
is NOT in PACKAGE_LIST
). I've tried adding a $
to the string comparison, however this only works if PACKAGE_LIST
ends in the PACKAGE_NAME
. Instead, PACKAGE_LIST
has a version appended to it. How can I make make this check smarter so that the check falls into "PACKAGE_NAME
is NOT in PACKAGE_LIST
"?
NOTE: sometimes PACKAGE_LIST
will not contain a version and will look like:
PACKAGE_LIST="rpm-foo-bar"
Upvotes: 1
Views: 1302
Reputation: 23850
How about
[[ ${PACKAGE_LIST} =~ "${PACKAGE_NAME}:" || ${PACKAGE_LIST} == ${PACKAGE_NAME} ]]
Upvotes: 2