elements not getting appended to array inside the block

Issue

I’ve created a script that basically gets the status codes on the url.
I’am trying to add the status code if it’s greater then 399.

The elements are getting appended to array correctly outside the for loop block but not inside of it.
(here that array is TP)

ARR=('http://google.com' 'https://example.org/olol' 'https://example.org' 'https://steamcommunity.com')
TP=()
TP+=("77")
ERROR=true

#for item in "${ARR[@]}";do curl -I $item | grep HTTP | awk '{print $2}'; sleep 3; echo $item; done;

for item in "${ARR[@]}";
        do curl -I $item | grep HTTP | awk '{print $2}'| { read message;
                echo "hi $message";
                TP+=("57")
                if [ $message -gt 399 ]; then
                        #TP+=("57");
                        ERROR=false;
                        echo "$message is greater";
                fi
        };
                sleep 2;
                echo $item;
done;

echo "${TP[@]}"

please help, I am noob.

Solution

When you pipe results (eg, from the curl call) to another command (eg, grep/awk/read) you are spawning a subshell; any variables set within the subshell are ‘lost’ when the subshell exists.

One idea for fixing the current code:

for item in "${ARR[@]}";
do
    read -r message < <(curl -I "$item" | awk '/HTTP/ {print $2}')
    echo "hi $message";
    TP+=("57")

    if [ "$message" -gt 399 ]
    then
        #TP+=("57")
        ERROR=false
        echo "$message is greater"
    fi

    sleep 2
    echo "$item"
done

Where:

  • while the curl|awk is invoked as a subshell the result is fed to the read in the current/parent shell, so the contents of the message variable are available within the scope of the current/parent shell
  • added double quotes around a few variable references as good practice (eg, in case variable contains white space, in case variable is empty)

Answered By – markp-fuso

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published