I am trying to get this script (from https://phongthaicao.medium.com/running-the-sonar-build-wrapper-and-scanner-on-different-hosts-in-azure-pipelines-91a4572490ec) to run inside a Jenkins file:
for cwd in `grep -oP '\"cwd\"\:\".*\"' $wrapperFile | sort -u`; do
cwd=${cwd:7:-1}
mkdir -p $cwd
done
This is about extracting the path from lines of the form:
"cwd":"/home/jenkins\\workspace\\xyx_sandbox_sq-test-updated\\",
Inside the Jenkins file the script is enclosed thusly:
script {
sh """
for cwd in `grep -oP '\\"cwd\\"\\:\\".*\\"' my-file.json | sort -u`; do
cwd=\${cwd:7:-1}
mkdir -p $cwd
done
"""
}
But however I try to configure the line cwd=${cwd:7:1}
(eg as above or cwd=\${\$cwd:7:1}
or countless other ways) I am generally getting a failure with Bad substitution
- could someone tell me what the canonical way to do this is? Do I need to do some further interpolation with the matched string (ie cwd
to escape the " before trying the substition?). I can tell that the grep is picking the correct lines, but after that it fails.
Thanks in advance.