Bash Scripting: Adding runtime in a progress bar

I have the following code that generates a progress bar and
want to add the current execution time after the percentage
value. The current execution time is stored in the variable `runtm`

I am having a problem on how to add `runtm` in the last `printf`
or after it.

   i=0; j=0
   totfcn=$(find $ipath -type f -name *hhz*.sac | tee /tmp/wrk | wc -l)

   for stfl in $(find $ipath -type f -name *hhz*.sac); do 
      printf -v XXX "%0*d" $((60 * ++i / totfcn))
      printf "\r[%-60s]" "${XXX//0/*}"
      printf "%4d%%" $((100 * ++j / totfcn))
   done

The current execution time could be formatted in grossly different ways that would require different format specifiers in a printf format string.

What is the format of the contents of the variable named runtm ?

Show us some example values that might be assigned to runtm and show us the exact output you hope to produce when the variable j has the value 20, the variable totfcn has the value 50, and the variable runtm has the various example values you have provided.

Also show us how you have tried to modify the final printf statement in your code (and the output you got with the code you tried).

The runtime will be of the format `hh:mm:ss`

runtm: 05:34:21

I would like things to look as follows

[*********************                     ]   55%   05:34:21

I have tried

printf "%4d%10s" $((100 * ++j / totfcn)), $runtm

and

printf "%4d%%" $((100 * ++j / totfcn))
printf "%10s" $runtm

---------- Post updated at 06:57 AM ---------- Previous update was at 05:16 AM ----------

There was a problem with one of the variables, it works ok now. So the commands are
working as they should.

You can combine the two working printf statements that are working into a single printf statement. The way you tried to do it didn't work because:

  • changing %% to %10s in the format statement caused it to stop printing a percent sign after the percentage completed, and
  • adding a comma after the percentage calculation either served as a no-op or caused a syntax error while trying to print a non-numeric string value with a %d format specifier (depending on which implementation of printf you invoked in your script).

To get the same output as that produced by:

printf "%4d%%" $((100 * ++j / totfcn))
printf "%10s" $runtm

with a single printf statement, use:

printf "%4d%%%10s" $((100 * ++j / totfcn)) $runtm

Note that this concatenates the two format strings into a single format string without otherwise changing any of the characters in either of the original format strings, and adding operands to be printed by each of the printf s being combined as operands in the same sequences in the combined printf command.