0

I would like to show a progress bar while running the following code. I'm calling a script within this code that could take a while depending on the variables passed in. I've tried some of the implementations from How to add a progress bar to a shell script? but have not been able to get them to work. In theory it should continue based off of whether or not the process is still running. If it's still running then show a progress bar.

cat $BLKUPLD | tr -d '\r' | while read line;
do
device_name=`echo $line | cut -d "," -f 1`
quantity_num=`echo $line | cut -d "," -f 2`
bash $SCRIPT $device_name $quantity_num
done

Please let me know if you need additional information.

  • 1
    Possible duplicate of [How to add a progress bar to a shell script?](https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script) – John Stark Jul 10 '18 at 14:58
  • I've tried some of those implementations but was not able to get any of them to work. – joshuaspatrick Jul 10 '18 at 15:06
  • Do you have a way to know when your process will end ? If not, how do you expect your progress bar to behave ? – Aserre Jul 10 '18 at 15:16
  • If you want something with a heavier interface, you can also have a look at `zenity`. [Here](https://askubuntu.com/questions/930994/how-to-make-a-progress-bar-work-in-zenity) are some examples of its use. – Aserre Jul 10 '18 at 15:21
  • Replace `cat` with `pv` – that other guy Jul 10 '18 at 17:25
  • Aserre - Each instance of $SCRIPT uses cURL to do a GET request. Then output some data after comparing the data received back. If that could be used maybe it could be displayed over each instance of the loop. Otherwise I would be fine with an "infinite" repeating progress bar until the script is completed running. – joshuaspatrick Jul 10 '18 at 17:31
  • I did try replacing cat with pv but it didn't work as expected and only worked on the first instance (when it loaded the source file) and not for the remaining loops. – joshuaspatrick Jul 10 '18 at 17:32

1 Answers1

0

Below is a progress bar script that will run until the bar is filled.

You will want to change the condition of the outermost while loop to instead check whether your $BLKUPLD has completed, and move the rest of your code inside the while loop where indicated (essentially, you may need to change MAX_ITERATIONS to a boolean condition, and play with where to insert the components of your code within this scaffold).

Ideally you would know how much remaining data you had, and could dynamically set MAX_ITERATIONS accordingly as you enter the loop logic; however, you mentioned that you were okay with having an infinitely looping progress bar as well, which might be the way you have to go if you aren't able to pre-define the end point of your script.

The main premise behind this script that differs from the other thread I linked, is that there are no hardcoded progress points: e.g. "[###__________]33%". Instead, the nested while loops serve to dynamically set the number of hashtags, and also dynamically pad the spacing following the hashtags to maintain a consistent span of progress.

#!/bin/sh
MAX_ITERATIONS=10
WHILE_ITERATOR=0
while [ $WHILE_ITERATOR -le $MAX_ITERATIONS ]

# __Add call to process checking script here.__

do
  # Appending hashtags to progress bar string.
  PROGRESS_BAR="["
  HASHTAGS=$WHILE_ITERATOR
  HASHTAGS_ITERATOR=0
  while [ $HASHTAGS_ITERATOR -le $HASHTAGS ]
  do

    # Accounting for first pass through outer loop.
    if [ $WHILE_ITERATOR -eq 0 ]; then
    PROGRESS_BAR+=" #"
    else
    PROGRESS_BAR+="#"
    fi
    HASHTAGS_ITERATOR=$((HASHTAGS_ITERATOR+1))
  done

  # Appending trailing spaces to progress bar string.
  SPACES=$((MAX_ITERATIONS-WHILE_ITERATOR-1))
  SPACES_ITERATOR=0
  while [ $SPACES_ITERATOR -le $SPACES ]
  do
    PROGRESS_BAR+=" "
    SPACES_ITERATOR=$((SPACES_ITERATOR+1))
  done

  # Closing progress bar screen and adding return esc char.
  PROGRESS_BAR+="]\r"

  # Setting echo -n to run properly on Unix & Mac
  if [ "`echo -n`" = "-n" ]; then
  n=""
  c="\c"
  else
  n="-n"
  c=""
  fi

  # Print the progress bar without \n; reprints in place.
  echo $n "$PROGRESS_BAR" $c
  sleep 1
  WHILE_ITERATOR=$((WHILE_ITERATOR+1))
done

# Print final iteration of progress bar string.
echo "$PROGRESS_BAR"
John Stark
  • 1,293
  • 1
  • 10
  • 22
  • Where you have the #_Add call... comment do you mean to check that the script is running (as in background it, run the progress bar script and check if still running or not) or actually place the code for the script in that section editing the while loop in my code to determine if all lines have been read in? Which would be best practice if they are both legitimate methods? – joshuaspatrick Jul 19 '18 at 20:37
  • The original way I had thought of it, was that you would start your code to read in lines, then start the progress bar code, and where the #_Add call annotation is, you would execute your 'listener' function to check if the line reading process was finished. If you did it this way, you would want to update the while loop condition to just be "while true" and then set up exit logic in your check script to run break [ 0 ] when the lines are finished reading. – John Stark Jul 20 '18 at 16:34