0

How can I use this sh script with for loop and an array. I would like to call create condition for quality gate creation of sonarqube with a for loop. Example:

#!/bin/bash --login

echo "Creating SonarQube Gateway Condition"

QG_ID=$(cat qualitygate.json | jq -r ".id")
Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST "${SONAR_HOST_URL}/api/qualitygates/create_condition?"
declare -a gateMetrics=("gateId=$QG_ID&metric=coverage&op=LT&error=80\"" "gateId=$QG_ID&metric=duplicated_lines_density&op=GT&error=10\"")

for val in "${gateMetrics[@]}"
do
  echo $Gateway$val
done

I want the output as below after running above command

curl -u ${USERNAME}:${PASSWORD} -k -X POST "${SONAR_HOST_URL}/api/qualitygates/create_condition?gateId=$QG_ID&metric=coverage&op=LT&error=80"
5
  • 1
    There is a quoting issue with: Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST "${SONAR_HOST_URL}/api/qualitygates/create_condition?". Use: Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST \"${SONAR_HOST_URL}/api/qualitygates/create_condition?\"" – user3188140 Jan 25 at 10:08
  • 1
    Have your run it through shellcheck? – ctrl-alt-delor Jan 25 at 10:13
  • Can you help me – Ridhika Dhingra Jan 25 at 10:36
  • In your gateMetrics, why is there a one escaped double quote? Where's the matching double quote? – glenn jackman Jan 25 at 14:17
  • The other escaped double quote should be in $Gateway (see my answer below) right after "POST". – user3188140 Jan 25 at 17:03
0

Variables for USERNAME; PASSWORD and QG_ID added for a self running example:

#!/bin/bash --login

USERNAME=foo
PASSWORD=bar
SONAR_HOST_URL="https://sonar.example.com"

echo "Creating SonarQube Gateway Condition"

# QG_ID=$(cat qualitygate.json | jq -r ".id")
QG_ID=123456

Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST \"${SONAR_HOST_URL}/api/qualitygates/create_condition?"
declare -a gateMetrics=("gateId=$QG_ID&metric=coverage&op=LT&error=80\"" "gateId=$QG_ID&metric=duplicated_lines_density&op=GT&error=10\"")

for val in "${gateMetrics[@]}"; do
  echo "$Gateway$val"
done

Output:

Creating SonarQube Gateway Condition
curl -u foo:bar -k -X POST "https://sonar.example.com/api/qualitygates/create_condition?gateId=123456&metric=coverage&op=LT&error=80"
curl -u foo:bar -k -X POST "https://sonar.example.com/api/qualitygates/create_condition?gateId=123456&metric=duplicated_lines_density&op=GT&error=10"
1
  • I already tried this and got below error ---Syntax error: Unterminated quoted string at Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST \"${SONAR_HOST_URL}/api/qualitygates/create_condition?" – Ridhika Dhingra Jan 25 at 12:47
0

You could break it down into smaller pieces.

#!/bin/bash --login

echo "Creating SonarQube Gateway Condition"

curl_cmd=( curl -u "${USERNAME}:${PASSWORD}" -k -X POST )
url="${SONAR_HOST_URL}/api/qualitygates/create_condition"
qg_id=$( jq -r ".id" qualitygate.json )
gateMetrics=(
    coverage,LT,80
    duplicated_lines_density,GT,10
)
query_fmt="gateId=%s&metric=%s&op=%s&error=%s"

for metric in "${gateMetrics[@]}"; do
    IFS=, read -r m o e <<< "$metric"
    printf -v query "$query_fmt" "$qg_id" "$m" "$o" "$e"

    # remove this echo if it looks OK
    echo "${curl_cmd[@]}" "${url}?${query}"
done

You'll also want to read I'm trying to put a command in a variable, but the complex cases always fail!

8
  • Error at : curl -u ****:**** -k -X POST: not found – Ridhika Dhingra Jan 27 at 7:44
  • Did you use the curl_cmd array like I did, or did you use a string? Read the link I posted – glenn jackman Jan 27 at 14:32
  • I used it in the same way you shared and it failed due to "curl -u ****:**** -k -X POST: not found " at curl_cmd step only – Ridhika Dhingra Jan 28 at 4:31
  • You must have added quotes somewhere, or did not declare "curl_cmd" as an array. The method I use in my answer will not try to run curl and its arguments concatenated together as a single program name. Check again – glenn jackman Jan 28 at 15:45
  • Hi, I copied the same thing which you shared and found syntax error as .......script.sh: Syntax error: "(" unexpected – Ridhika Dhingra Feb 2 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.