简体   繁体   English

如何在bash脚本的grep regex字符串中使用变量

[英]How to use a variable in a grep regex string in a bash script

I have a script that I am using to download EJBCA Community. 我有一个用于下载EJBCA社区的脚本。 They do something a little different where they put a minor version inside of a major version. 他们在将次要版本放入主要版本的地方做了一些不同的事情。 For example EJBCA Community 6.10.0 has a minor version inside that folder of 6.10.1.2. 例如,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

The problem I am having is specifically the grep -o -i "ejbca_ce_$EJBCA_BUILD_[0-9].zip" part. 我遇到的问题是grep -o -i“ ejbca_ce_ $ EJBCA_BUILD_ [0-9] .zip”部分。 I cannot get the variable inside the regex to work. 我无法使正则表达式中的变量正常工作。 I assume this is because its parsing it incorrectly and the variable comes up empty. 我认为这是因为它不正确地对其进行了解析,并且变量为空。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Use curly braces to ensure your variable name is what you intended, eg, ${EJBCA_BUILD} - in this case, I suspect that because underscore ( _ ) is a valid variable character, you are unintentionally using the variable $EJBCA_BUILD_ which is unset, so is replaced by an empty string, resulting in your grep expression being grep -o -i "ejbca_ce_[0-9].zip" which isn't what you want. 使用大括号确保变量名是您想要的名称,例如${EJBCA_BUILD} -在这种情况下,我怀疑由于下划线( _ )是有效的变量字符,因此您无意中使用了未设置的变量$EJBCA_BUILD_ ,所以用空字符串替换,导致您的grep表达式不是您想要的grep -o -i "ejbca_ce_[0-9].zip"

You can see this by doing a simple test in your shell: 您可以通过在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM