Timer running under bash script in dialog window

2

1

The following bash script is an example on how to use the dialog command. This script runs a progress bar (and displays the process upgrade). What is missing in the dialog is a time clock - that displays how much time the progress bar has been running, every second, until the end.

I am not sure if the dialog enables this, so I ask if it is possible to print the clock inside the dialog window?

If not, what are other alternatives? (for example, a clock that runs outside the dialog window)

  #!/bin/bash
  declare PACKAGES=("/etc/crontab"  "/etc/dmtab"  "/etc/fstab"  "/etc/inittab"  "/etc/mtab")
     NUM_PACKAGES=${#PACKAGES[*]} # no. of packages to update (#packages in the array $PACKAGES)
  step=$((100/$NUM_PACKAGES))  # progress bar step
   cur_file_idx=0
   counter=0
 DEST=${HOME}
    (
    # infinite while loop
    while :
  do
  cat <<EOF
  XXX
 $counter
   $counter% upgraded

   $COMMAND
   XXX
   EOF
       COMMAND="cp ${PACKAGES[$cur_file_idx]} $DEST &>/dev/null" # sets/updates command to exec.
      [[ $NUM_PACKAGES -lt $cur_file_idx ]] && $COMMAND # executes command

   (( cur_file_idx+=1 )) # increase counter
     (( counter+=step ))
   [ $counter -gt 100 ] && break  # break when reach the 100% (or greater
                               # since Bash only does integer arithmetic)
   sleep 10 # delay it a specified amount of time i.e. 1 sec
 done
     ) |
      dialog --title "File upgrade" --gauge "Please wait..." 10 70 0

UPDATE: I also found some timer code and I want to combine this code in the dialog line - how do I do this?

The timer script (code) :

 date1=`  date   +%s`; 
   while true; do 
   echo -ne "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)\r"; 
   done

maihabunash

Posted 2014-09-18T09:04:05.107

Reputation: 479

I don't believe a countdown timer is possible, as the system on which this is running is likely to be non-deterministic... – Jan – 2014-09-18T09:14:45.623

Answers

1

Indeed it would be nice if dialog had a --show-elapsed option or something, it's not straightforward to do this currently.

The display part is not too hard: you can (ab)use the --title option to display the elapsed time. You can even get it to display below the gauge by adding more \n (and changing the box size).

enter image description here

More tricky is to get it to display every second even if there's 50s between status updates. Here's a solution using read -t timeout option:

#!/bin/bash

show_dialog()
{
    p=0             # percentage
    date1=`date +%s`
    while [ "$p" != 100 ]; do
        read -t 1 tmp && p=$tmp
        elapsed="$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)"
        echo $p | dialog --title "File upgrade" --gauge "Please wait...\n\n\n\n$elapsed" 10 70 0
    done
}

task()              # fake task
{ for p in `seq 1 100`; do echo $p; sleep 2; done; }

task | show_dialog

lemonsqueeze

Posted 2014-09-18T09:04:05.107

Reputation: 1 151