1

Using bash I have a string that holds my deployed applications:

APPS="myapp-a-1.0.war myapp-b-1.1.war myapp-c-1.2-SNAPSHOT.war"

I can determine whether a specific app is deployed:

if [[ "$APPS" == *myapp-a* ]]
then
    echo "myapp-a is deployed, but we don't know the version."
fi

How can I retrieve the complete matching word (application with version) given that I only have the words prefix (application name, e.g. 'myapp-a')?

2 Answers 2

1

You can loop over your variable (provided you don't quote it):

for app in ${APPS}; do
  if [[ "${app}" == myapp-a* ]]; then
    echo ${app}
  fi
done
Sign up to request clarification or add additional context in comments.

Comments

1

as simple as:

if [[ "$APPS" =~ myapp\-a([^war]*)war ]]; then
  echo "${BASH_REMATCH[0]} deployed"
fi

result in:

myapp-a-1.0.war deployed

if you need only version:

if [[ "$APPS" =~ myapp\-a([^war]*)war ]]; then
  echo "${BASH_REMATCH[1]} deployed"
fi

result in:

-1.0. deployed

if you need version without artefacts like dashes, dots:

if [[ "$APPS" =~ myapp\-a\-([^war]*)\.war ]]; then
  echo "${BASH_REMATCH[1]} deployed"
fi

result in:

1.0 deployed

1 Comment

Thanks. These are great examples provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.