1

使用 bash 我有一个字符串来保存我部署的应用程序:

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

我可以确定是否部署了特定的应用程序:

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

鉴于我只有单词前缀(应用程序名称,例如'myapp-a'),我如何检索完整的匹配单词(带有版本的应用程序)?

4

2 回答 2

1

您可以遍历您的变量(前提是您不引用它):

for app in ${APPS}; do
  if [[ "${app}" == myapp-a* ]]; then
    echo ${app}
  fi
done
于 2012-10-05T06:56:52.053 回答
1

很简单:

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

导致:

myapp-a-1.0.war deployed

如果您只需要版本:

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

导致:

-1.0. deployed

如果您需要没有破折号、圆点等伪像的版本:

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

导致:

1.0 deployed
于 2012-10-05T09:52:22.103 回答