0

How can I use this shell 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"
6
  • 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?\"" Commented Jan 25, 2021 at 10:08
  • 2
    Have your run it through shellcheck? Commented Jan 25, 2021 at 10:13
  • Can you help me
    – user1628
    Commented Jan 25, 2021 at 10:36
  • In your gateMetrics, why is there a one escaped double quote? Where's the matching double quote? Commented Jan 25, 2021 at 14:17
  • The other escaped double quote should be in $Gateway (see my answer below) right after "POST". Commented Jan 25, 2021 at 17:03

2 Answers 2

1

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
    – user1628
    Commented Jan 27, 2021 at 7:44
  • Did you use the curl_cmd array like I did, or did you use a string? Read the link I posted Commented Jan 27, 2021 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
    – user1628
    Commented Jan 28, 2021 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 Commented Jan 28, 2021 at 15:45
  • Hi, I copied the same thing which you shared and found syntax error as .......script.sh: Syntax error: "(" unexpected
    – user1628
    Commented Feb 2, 2021 at 9:16
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?"
    – user1628
    Commented Jan 25, 2021 at 12:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.