A Torre A Torre - 11 months ago 105
Bash Question

Shell Regex not matching

I need to extract the version from a line in shell scrip but my regex is not picking up the correct value.

this is my script:

versionLine="Creating version 1.0.74 ++ grep 'Creating version ' /tmp/apps/runtime/data/jenkins/jobs/myJob/build/68/log"
re=".*Creating version (.*) \+\+.*"

if [[ ${versionLine} =~ $re ]]
then
echo FOUND: ${BASH_REMATCH[1]}
fi


Code does not go inside the
if
. I would like to echo:
FOUND: 1.0.74

Answer Source

Your script uses bash-only syntax, but does not have a shebang.

Add to the top:

#!/usr/bin/env bash

(or #!/bin/bash, as appropriate)

...and ensure that you don't invoke it with sh yourscript.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download