From: Fox on
I've shell loop in which I display 10 lines of the file at a time.
I've code line as follows to pause user:
---- My bash under GNU/Linux loop code ---
while read l
do
if [ $c -eq 10 ]
then
c=1 # reset
echo " *** Press [Enter] key to continue ..."
read anyDamkey
fi
echo $l
(( c++ ))
done < $input


I only see message *** Press [Enter] key to continue ... and read
command skips and loop continue while end of file. It appears that \n
from previous echo $line is feeding read command. In others word I'd
like to flush buffer and wait for pause prompt. How do I fix this. I
spend 4 hours but with no luck :(

~~~~~~~
My output
-----------
line 1 to 10
*** Press [Enter] key to continue .. <--- not wating for use input
line 11 to 20
*** Press [Enter] key to continue .. <--- not wating for use input
line 11 to N



TIA

Fox
From: Barry Margolin on
In article
<1b4253b6-dc95-42bd-ab8f-ec1422ab34b3(a)n1g2000prb.googlegroups.com>,
Fox <fox(a)foxmail.in> wrote:

> I've shell loop in which I display 10 lines of the file at a time.
> I've code line as follows to pause user:
> ---- My bash under GNU/Linux loop code ---
> while read l
> do
> if [ $c -eq 10 ]
> then
> c=1 # reset
> echo " *** Press [Enter] key to continue ..."
> read anyDamkey
> fi
> echo $l
> (( c++ ))
> done < $input
>
>
> I only see message *** Press [Enter] key to continue ... and read
> command skips and loop continue while end of file. It appears that \n
> from previous echo $line is feeding read command. In others word I'd
> like to flush buffer and wait for pause prompt. How do I fix this. I
> spend 4 hours but with no luck :(
>
> ~~~~~~~
> My output
> -----------
> line 1 to 10
> *** Press [Enter] key to continue .. <--- not wating for use input
> line 11 to 20
> *** Press [Enter] key to continue .. <--- not wating for use input
> line 11 to N
>
>
>
> TIA
>
> Fox

Standard input is being redirected, so all the read commands within the
while loop read from the file. The redirection doesn't just apply to
the read command on the while statement itself.

If you want to read from the original stdin before the redirection, you
need to save it:

exec 3<&0
while read l
do
...
read -u 3 anyDamkey
...
done < $input

Or if you specifically want to read from the terminal, use

read anyDamkey </dev/tty

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Bill Marcum on
On 2008-04-21, Fox <fox(a)foxmail.in> wrote:
>
>
> I've shell loop in which I display 10 lines of the file at a time.
> I've code line as follows to pause user:
> ---- My bash under GNU/Linux loop code ---
> while read l
> do
> if [ $c -eq 10 ]
> then
> c=1 # reset
> echo " *** Press [Enter] key to continue ..."
> read anyDamkey
> fi
> echo $l
> (( c++ ))
> done < $input
>
>
> I only see message *** Press [Enter] key to continue ... and read
> command skips and loop continue while end of file. It appears that \n
> from previous echo $line is feeding read command. In others word I'd
> like to flush buffer and wait for pause prompt. How do I fix this. I
> spend 4 hours but with no luck :(
>
By default the read command reads from standard input, which you have
redirected.
Possible solutions are:
read anyDamkey </dev/tty
read anyDamkey <&2

exec 3<&0
while read l
....
read anyDamkey <&3
....
done <$input

exec 3<$input
while read l
....
done <&3

From: Stephane CHAZELAS on
2008-04-21, 02:55(-04), Barry Margolin:
[...]
> exec 3<&0
> while read l
> do
> ...
> read -u 3 anyDamkey
> ...
> done < $input
[...]

Or, more logically to my mind:

while read <&3 l; do
...
read anyDamLine
...
done 3< "$input"

Note that -u 3 is a ksh extension also found in bash and zsh, I
don't really see the advantage over the standard <&3 (other than
it allows for fds over 9).

--
St�phane
From: Fox on
thank for all your input :) it was really hard to find out the
solution just reading man pages.