Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause for loop until key is pressed

Tags:

bash

for-loop

How can I run a for loop which pauses after each iteration until a key is pressed? for example, if I wanted to print the line number of file1, file2, file3, but only continuing each after pressing a key:

for f in dir/file? ; do wc -l $f ; pause until key is pressed ; done

Apologies if this is trivial, I'm new to the coding.

like image 671
Stewart Russell Avatar asked Feb 14 '17 19:02

Stewart Russell


1 Answers

Use the read command to wait for a single character(-n1) input from the user

read -p "Press key to continue.. " -n1 -s

The options used from the man read page,

-n nchars return after reading NCHARS characters rather than waiting 
          for a newline, but honor a delimiter if fewer than NCHARS
          characters are read before the delimiter

-s      do not echo input coming from a terminal

-p      prompt output the string PROMPT without a trailing newline before
        attempting to read
like image 181
Inian Avatar answered Sep 28 '22 13:09

Inian