I am working on shell script that iterates through a json response and based on a condition I am setting the values to a variable inside the for loop.
I have created a variable outside the for loop, and inside the for loop I print the value and it is printing the right values, but when I print the same variable outside the for loop it prints empty values.
export critical_violations=''
export major_violations=''
for k in $(jq '.component.measures | keys | .[]' <<< "${violations}"); do
metricData=$(jq -r ".component.measures[$k]" <<< "${violations}");
metric=$(jq -r '.metric' <<< "$metricData");
value=$(jq -r '.value' <<< "$metricData");
if [ "$metric" = "major_violations" ]; then
major_violations=`echo ${value}`
echo "Major violation count : ${value}"
elif [ "$metric" = "critical_violations" ]; then
critical_violations=`echo ${value}`
echo "Critical violation count : ${value}"
fi
echo "$metric""=""$value" >> "sonarQube_results.txt"
done | column -t -s$'\t'
echo "Report Url = ${dashboardURL}" >> "sonarQube_results.txt"
sonarqubescaninfo="$(cat sonarQube_results.txt)"
echo "Scan Results : "
echo "${sonarqubescaninfo}"
echo "critical violations count : $critical_violations"
export critical_violations_count=(`echo ${critical_violations}`)
export major_violations_count=(`echo ${major_violations}`)
export critical_violations_threshold=(`echo ${COMPLIANCE_CRITICAL_SEVERITY_COUNT}`)
export major_violations_threshold=(`echo ${COMPLIANCE_MAJOR_SEVERITY_COUNT}`)
Below lines are printing empty values
export major_violations_count=(`echo ${major_violations}`)
echo "critical violations count : $critical_violations"
I need these values to perform a value check to exit the process
if [ $critical_violations_count -gt $critical_violations_threshold ]; then
echo "Exiting the build as sonarqube scan critical violations count are beyond the acceptable vulnerability threshold"
exit -1
elif [ $major_violations_count -gt $major_violations_threshold ]; then
echo "Exiting the build as sonarqube scan major violations count are beyond the acceptable vulnerability threshold"
exit -1
fi
can someone help me to fix this.