我有一个用于下载EJBCA社区的脚本。他们在将次要版本放入主要版本的地方做了一些不同的事情。例如,EJBCA Community 6.10.0在该文件夹6.10.1.2中有一个次要版本。
#Set build (This is actually passed as a command line argument in the main script. But for clarity I am putting it here like this.
EJBCA_BUILD=6.10.0
#Check to see if standard version URL works (no minor patched version)
STATUS="$(curl -s --head -w %{http_code} "https://sourceforge.net/projects/ejbca/files/ejbca6/ejbca_$EJBCA_BUILD/ejbca_ce_$EJBCA_BUILD.zip" -o /dev/null)"
echo $STATUS
#If it doesn't exist grep to find out what the patched version is and create $EJBCA_File with the version.
if [ $STATUS = 404 ]; then
EJBCA_FILE="$(curl -s -L "https://sourceforge.net/projects/ejbca/files/ejbca6/ejbca_$EJBCA_BUILD/ejbca_ce_$EJBCA_BUILD/" | grep -o -i "ejbca_ce_$EJBCA_BUILD_[0-9].zip" | head -n1)"
#Bring the URL together so we can download it.
EJBCA_URL=https://sourceforge.net/projects/ejbca/files/ejbca6/ejbca_$EJBCA_BUILD/$EJBCA_FILE.zip
elif [ $STATUS = 200 ]; then
# If it works then download it as is.
EJBCA_URL=https://sourceforge.net/projects/ejbca/files/ejbca6/ejbca_$EJBCA_BUILD/ejbca_ce_$EJBCA_BUILD.zip
else
echo -n "There was some other error returned from the server than 404 or 200. Exiting."
echo -n "The error code was $STATUS"
exit 1
fi
我遇到的问题特别是grep -o -i“ ejbca_ce_ $ EJBCA_BUILD_ [0-9] .zip”部分。我无法使正则表达式中的变量正常工作。我认为这是因为它不正确地对其进行了解析,并且变量为空。任何帮助将不胜感激。
答案 0 :(得分:2)
使用大括号确保变量名是您想要的,例如${EJBCA_BUILD}
-在这种情况下,我怀疑由于下划线(_
)是有效的可变字符,因此您无意中使用了变量$EJBCA_BUILD_
未设置,因此被空字符串替换,导致您的grep
表达式不是grep -o -i "ejbca_ce_[0-9].zip"
。
您可以通过在shell中进行一个简单的测试来看到这一点:
$ EJBCA_BUILD=dummy
$ echo "ejbca_ce_$EJBCA_BUILD_[0-9].zip"
ejbca_ce_[0-9].zip
$ echo "ejbca_ce_${EJBCA_BUILD}_[0-9].zip"
ejbca_ce_dummy_[0-9].zip