我正在使用一些标准验证字符串。它应该以 R- 和 proj_nm 作为变量,project_cd 作为数字和任何 desc 开始,即 R-${PROJ_NM}-[Numbers]-anything
if [[ $var == "R-"${PROJ_NM}"-"[[:digit:]]"-"* ]] ; then echo "Naming convention is in correct format" else echo "Not in correct format" exit 1 fi
我得到的输出格式不正确。
答案1
如果您需要一个或多个号码(您说的是号码s),那么你需要[[:digit:]][[:digit:]]
两个,[[:digit:]][[:digit:]][[:digit:]]
三个等等。
if [[ $var == "R-"${PROJ_NM}"-"[[:digit:]][[:digit:]]"-"* ]] ; then
echo "Naming convention is in correct format"
else
echo "Not in correct format"
fi
但你可以稍微简化一下你的报价:
if [[ $var == "R-${PROJ_NM}-"[[:digit:]][[:digit:]]"-"* ]] ; then
echo "Naming convention is in correct format"
else
echo "Not in correct format"
fi
或者,对于较新的 bash 版本,您可以使用正则表达式:
if [[ $var =~ ^R-${PROJ_NM}-[0-9]+- ]] ; then
echo "Naming convention is in correct format"
else
echo "Not in correct format"
fi