0

Following this 2013 question, on displaying a countdown in a terminal, I would like to improve the answer a bit using pv to show a progression bar.

Here is my current script.

function countdown {
   date1=$((`date +%s` + $1));
   while [ "$date1" -ge `date +%s` ]; do
     echo -ne "$(date -u --date @$(($date1 - `date +%s`)) +%H:%M:%S)\r";
     sleep 0.1
   done
}

# 7 minutes countdown
countdown $((7*60))

How do I improve this with pv? It looks like it tracks progress measuring data written. In my case I just need to wait and I don't have indicators to measure.

Maybe there are best alternatives as of 2018?

Thanks.

Michel Hua
  • 1,614
  • 2
  • 23
  • 44

2 Answers2

1

Just do it this way with pv, writing one Byte of character every second.

secs=$((7 * 60))
while [ $secs -gt 0 ]
do 
  echo -n "."
  sleep 1
  : $((secs--))
done | pv -s $secs > /dev/null

echo -n is for

  -n     do not output the trailing newline

pv -s is for

  -s, --size SIZE          set estimated data size to SIZE bytes

sleep 1; : $((secs--)) to decrement the value of $secs every 1 second

Michel Hua
  • 1,614
  • 2
  • 23
  • 44
  • Start with 1MB file, divide it by number of secs to obtain several chunks, then write the file concatenate the file back ? – Michel Hua Aug 27 '18 at 22:17
  • Better that way. But still, this is like using a hammer to pin a screw. `secs=10; while [ "$secs" -gt 0 ]; do echo -n "."; sleep 1; secs=$((secs-1)); done | pv -s "$secs" > /dev/null` – Poshi Aug 28 '18 at 08:06
0

You cannot improve this with pv. pv stands for Pipe View, and it tracks the progress of a data flow. Knowing the size and measuring the quantity of data passingh through it, it can give you an estimate of remaining time. If you just need a timer countdown, pv is useless.

You already have something written that can be embellished. Other scripts for your need exists in the web: http://handybashscripts.blogspot.com/2012/01/simple-timer-with-progress-bar.html

Poshi
  • 5,332
  • 3
  • 15
  • 32
  • But can I artificially build a file that takes exactly 7 min to process? – Michel Hua Aug 26 '18 at 10:12
  • That would be overkill and innacurate. The file could take too much storage space and the timing will depend on the variable speed of the underlying file system. Probably you could manage to do something in that way, but it won't be accurate and, for sure, it won't be an improvement. – Poshi Aug 26 '18 at 10:14
  • Or `curl` a webservice that timeouts after exactly 7 minutes ? – Michel Hua Aug 26 '18 at 10:15
  • 2
    Are you really considering an improvement having to implement a webservice that times out after 7 minutes against a simple `sleep`??? – Poshi Aug 26 '18 at 10:16
  • I think I might have an answer, please have a look. – Michel Hua Aug 27 '18 at 22:11
  • There is one webservice that does exactly that ah ah https://lifehacker.com/5610036/use-a-simple-web-app-as-a-live-countdown-timer My initial problem was to wait to show a webpage. I must wait 7 min for the infrastructure to bootstrap. I can basically do a command line curl pv with this or do an HTML redirect in JavaScript after 7 min. – Michel Hua Aug 29 '18 at 18:08