So, I want to write a bash script that are a sequence of steps and ill identify it as "task#". However, each step is only completed and can run as long as the user wants.
Do task1
if keypressed stop task1 and move on #this is the part I need help with. There can be up to 10 of these move on steps.
Do task2
...
kina like top; it keeps doing stuff until you hit q to quite, however, i want to move on to the next thing
kev's great solution works well even in Bash 3.x., but it introduces a 1-second delay (
-t 1
) in every loop iteration.In Bash 3.x, the lowest supported value for
-t
(timeout) is1
(second), unfortunately.Bash 4.x supports
0
and fractional values, however:A solution that supports an arbitrary key such as
q
requires a nonzero-t
value, but you can specify a value very close to0
to minimize the delay:Caveat: The above uses
0.01
as the almost-no-timeout value, but, depending on your host platform, terminal program and possibly CPU speed/configuration, a larger value may be required / a smaller value may be supported. If the value is too small, you'll see intermittenterror setting terminal attributes: Interrupted system call
errors - if anyone knows why, do tell us.Tip of the hat to jarno for his help with the following:
Using
-t 0
, works as follows, according tohelp read
(emphasis added):As of Bash v4.4.12, unfortunately,
-t 0
seems to ignore-n
/-N
, so that only an ENTER keypress (or a sequence of keypresses ending in ENTER) causesread
to indicate that data is available.If anyone knows whether this is a bug or whether there's a good reason for this behavior, do let us know.
Therefore, only with ENTER as the quit key is a
-t 0
solution currently possible:you can use
read
builtin command with option-t
and-n