小编ven*_*ter的帖子

带有 \d 的正则表达式在带有 [[ 的 if-else 语句中不起作用

我写了以下脚本。稍后将在构建过程中使用它。我的目标是决定它是预发布版还是发布版。为了存档,我将 $release 与 RegEx 进行比较。如果我的 RegEx 匹配,则为预发布,否则为发布。

#/bin/bash
release="1.9.2-alpha1"
echo "$release"
if [[ "$release" =~ \d+\.\d+\.\d+[-]+.* ]];then
echo "Pre"
else
echo "Release"
fi
Run Code Online (Sandbox Code Playgroud)

但结果我总是得到以下结果:

~$ bash releasescript.sh
1.9.2-alpha1
Release
Run Code Online (Sandbox Code Playgroud)

版本:

Ubuntu 18.04.1 LTS
Run Code Online (Sandbox Code Playgroud)

我用这个编辑器来测试我的 RegEx。我被困了至少 6 小时,所以我非常感谢一些帮助。

command-line bash scripts regex

7
推荐指数
1
解决办法
1256
查看次数

标签 统计

bash ×1

command-line ×1

regex ×1

scripts ×1