Operating System - HP-UX
1836847 Members
2222 Online
110110 Solutions
New Discussion

User Input within a while loop

 
SOLVED
Go to solution
SHABU KHAN
Trusted Contributor

User Input within a while loop

Hi,
I have a while loop which reads three variables from a file.. and after some processing further down the program I am expecting the user to hit enter to continue...
"Press enter to continue ... " read
but, when I execute the program it doesn't stop where it is supposed to but just runs till the program finishes.. when I executed with debugging on... I found that the second read basically reads the three variables that I specified in the while loop.. ? How do I fix this ?
4 REPLIES 4
Steven Sim Kok Leong
Honored Contributor

Re: User Input within a while loop

Hi,

It will be clearer if you show us the code you have written.

The stub should go something like this:

#!/sbin/sh
# while loop runs until end of file
cat file | while read var1 var2 var3
do
# process var1 var2 var3
echo $var1 $var2 $var3
echo "Press any key to continue\c"
read
done

Hope this helps. Regards.

Steven Sim Kok Leong
SHABU KHAN
Trusted Contributor

Re: User Input within a while loop

Sure, here you go..

Please find attachment..

Thanks,
Steven Sim Kok Leong
Honored Contributor
Solution

Re: User Input within a while loop

Hi,

The command read reads from STDIN which is the input through the pipe

cat file | while ...

You can workaround this in your script as follows:

#!/sbin/sh

total=`cat inputfile|wc -l|awk '{print $1}'`
cnt=1
while [ "$cnt" -le "$total" ]
do
cat inputfile|tail +$cnt|head -1|read var1 var2 var3
print $var1 $var2 $var3
print "Press any key to continue...\c"
read
cnt=`expr $cnt + 1`
done

Hope this helps. Regards.

Steven Sim Kok Leong
SHABU KHAN
Trusted Contributor

Re: User Input within a while loop

Thanks for your assistance Steve !
Makes sense.. pretty neat workaround... I should have thought about this earlier :-)

-Shabu