0

Here is a snippet from my shell script -

regex="<version>([0-9]*)\.([0-9]*)\.([0-9]*)([^<]*)<\/version>.*"

#some unrelated lines of code

    echo $(grep -m 1 "<version>" $1)
    if [[ $(grep -m 1 "<version>" $1) =~ $regex ]]
    then
        main_version = "${BASH_REMATCH[1]}"
        feature_version = "${BASH_REMATCH[2]}"
        patch_version = "${BASH_REMATCH[3]}"
        tag = "${BASH_REMATCH[4]}"
    else
        echo "$1: Version not Found" >&2
    fi

But it is showing me the following output -

<version>1.1.7-release</version> <!-- ABC version -->
abc/xyz/pom.xml: Version not Found

But according to my understanding, regex should match with the output of grep(i echoed it to terminal for better clarity). So can someone tell what am i doing wrong?

3
  • I tried this on a file containing your stuff, and it matches... what is your bash release ? (bash --version)
    – tonioc
    Aug 1, 2018 at 17:46
  • Also, what exactly is the output from grep? echo $(grep ...) will mangle the output in a number of ways (in fact, echo $(somecommand) is almost always a bad idea). Try grep ... | LC_ALL=C cat -vet -- the cat command will convert a variety of normally-invisible characters to visible forms, and add a "$" to the end of each line so you can see where they are. Aug 1, 2018 at 19:06
  • I found the error, it was caused by export -f custom_function to use the custom_function in xargs and ofcourse the aboce snippet was part of custom_funcion. thanks for giving your time
    – monster
    Aug 1, 2018 at 19:10

0

You must log in to answer this question.

Browse other questions tagged .