I'm trying to read the output of a RESTful API status on the progress of a download and print that out. As soon as the download is finished the status changes from "running" to "terminated regularly" and the "progress" indicator is not shown anymore. Therefore the loop end with an empty variable $progress.
So the following script is correctly showing the progress of the download, but as soon as the download is finished the script ends abruptly and doesn't show the "Download finished."
Assume a download is running on session 1.
while true; do
status=$(curl -s -u admin:admin -H "Accept:application/json" -X GET \
-i http://1.2.3.4/sessionInformation/1/status | grep "Status" | sed 's/ "Status"://g' | sed 's/"//g')
while [[ $status != "terminated regularly" ]]; do
progress=$(curl -s -u admin:admin -H "Accept:application/json" -X GET \
-i http://1.2.3.4/sessionInformation/1/status | grep "Progress" | sed 's/["Progress":" %]//g')
echo -ne "Download ${progress} % finished.\r"
done
echo "Download finished."
break
done
Here's the output of the script running with "set -x":
+ progress=99
+ echo -ne 'Download 99 % finished.\r'
+ [[ running, != \t\e\r\m\i\n\a\t\e\d\ \r\e\g\u\l\a\r\l\y ]]
++ curl -s -u admin:admin -H Accept:application/json -X GET -i http://1.2.3.4/sessionInformation/1/status
++ grep Progress
++ sed 's/["Progress":" %]//g'
+ progress=
I'm running MacOS Ventura 13.4.1 and Bash 3.2.57(1).
I tried wrapping the whole thing in various if-statements and/or while loops, but no mater what else I read as an indicator of the download being finished and the status having changed, the problem is always the empty $progress variable. I also tried checking $progress using -z, but that didn't work. I just want the loop to dislay the progress and as soon as it's done downloading to proceed with whatever, at least printing another prompt, that it has finished.