I have a url that I need to execute using curl. If the status is 200 then write the response in a temporary file. Now compare this temporary file with another file ("/opt/proc/config/init.txt"). If the temporary file is different then replace content of init.txt with that temporary file. But if status is not 200 then exit with non zero status code with a message.
Below is what I have got. Is there any better or efficient way to do this? Also can this all be done in single line if possible?
URL="some_url"
# store the whole response with the status at the and
response=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $URL)
# extract the body
new=$(echo $response | sed -e 's/HTTPSTATUS\:.*//g')
# extract the status
status=$(echo $response | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
# print the body
echo "$new"
echo $new > /opt/proc/config/temp.txt
if [ $status -eq 200 ]; then
if ! cmp /opt/proc/config/init.txt /opt/proc/config/temp.txt > /dev/null 2>&1
then
echo different
mv /opt/proc/config/temp.txt /opt/proc/config/init.txt
else
echo same
fi
else
echo "Error [HTTP status: $status]"
rm /opt/proc/config/temp.txt
exit 1
fi
./script.sh.