1

I am trying to implement a waiting time the user can choose to skip by pressing any key. Also it must not print anything on the terminal as I am using it to display a progress bar. I am having difficulty to catch and test user input with the further difficulty to it must not induce more delay in the total waiting time.

Here is what I have at the moment :

read -t 0 input </dev/tty1
   if [[ $input -ne "" ]]; then
exit 0
fi

And that is inside a loop that update the progress bar. It succeeds not to add more delay or print unwanted lines but it doesn't fulfill its function.

-t 0 not to add further delay

reading from "/dev/tty1" not to add unwanted lines

How can I achieve this ?

Adrien Horgnies
  • 285
  • 1
  • 9

2 Answers2

2

Believe it or not, it's the zero that's messing you up. If you change it for a very low number like .01, it will work. You can stream line it even further like this:

if $(read -t.01 -sn1); then exit 0; fi
bashBedlam
  • 992
  • 7
  • 16
1

You want to run two things in parallel.

  1. Read the input waiting 5 sec (say)
  2. Update the progress bar ( showing 5 seconds are finishing up )

To achieve this, do not put the read command inside the loop.

Instead, do something like this :

for i in {1..10}; do sleep 0.5s; echo ${i}0; done | zenity --progress --auto-close --text="waiting for input...5sec" 2>/dev/null &  
read -t 5 asd && kill $!

( put the above two lines to a file (say abc.sh) and run it bash abc.sh )

Explanation :

The first command ( to update the progress bar )is sent to background with & . If read command if finished first, kill $! will close the progress bar.

Note: Checkout GNU Parallel. That would help you do more stuffs in parallel.

Severus Tux
  • 9,736
  • 9
  • 58
  • 97