0

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"

enter image description here

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.

  • 1
    `for do...done | columns` is putting the for-loop in a subshell, so changes inside of it won't be applied to the parent. – tjm3772 Mar 28 '23 at 13:39
  • 1
    Shells are designed or running other commands, not analyzing data. You would be better off using a general-purpose programming language for this. (As an aside, *none* of the variables you are using need to be exported.) – chepner Mar 28 '23 at 13:44
  • 2
    Why in the world are you using such a strange style for assigning values to variables? For example, what do you think `major_violations=\`echo ${value}\`` is gaining you over simply `major_violations=${value}`? – John Bollinger Mar 28 '23 at 13:56
  • 1
    And are you *intentionally* creating single-element arrays (`export critical_violations_count=(\`echo ${critical_violations}\`)`)? – John Bollinger Mar 28 '23 at 14:00
  • 1
    This is because the body of your loop runs in a child process. – user1934428 Mar 28 '23 at 14:02
  • ... which is because you are redirecting its output. – John Bollinger Mar 28 '23 at 14:03
  • Add a `jq` tag to your question. It seems you could do this mostly in `jq` , Good luck. – shellter Mar 28 '23 at 14:46
  • As an aside, probably try https://shellcheck.net/ before asking for human assistance. – tripleee Mar 28 '23 at 15:01

0 Answers0