0

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.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
  • Your script uses Bash features, but you are running it with `sh`. (Separately, using backslashes instead of forward slashes looks like an error.) – tripleee Sep 23 '22 at 15:50
  • You have various other stylistic issues which https://shellcheck.net/ will fix for you. Going forward, probably try this service before asking for human assistance. – tripleee Sep 23 '22 at 15:52

0 Answers0