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.
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:
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