Operating System - HP-UX
1835304 Members
2419 Online
110078 Solutions
New Discussion

script problem with read command...

 
SOLVED
Go to solution
MikeL_4
Super Advisor

script problem with read command...

I have a script that does a while read .... of a file and within there it does a function call to reply to a question being asked. The problem I am having is it gets to the second read statement and goes into a loop there. Is there a way around this or a better way to handle it ???

Basically I want to read through a file and ask if they want to process this record or not ....


while read turntype project filename revision
do
print "Processing line: "
print "Processing line: " >> ${PVCS_LOG5}
print "${turntype} ${project} ${filename} ${revision} "
print "${turntype} ${project} ${filename} ${revision} " >> ${PVCS_LOG5}
ask "Process this Regression Record? (Y/N) "
if [ $? -eq 0 ]
then
if [ ${TYPE} = "tupinstallprod" ]
then
......etc....


function ask
{
typeset rc
rc=2
while [ ${rc} -gt 1 ]
do
print -n ${1}
sleep 2
read answer
if [ "${answer}" = "Y" -o "${answer}" = "y" ]; then
rc=0
elif [ "${answer}" = "N" -o "${answer}" = "n" ]; then
rc=1
else
rc=2
fi
done
return ${rc}
}
5 REPLIES 5
Rodney Hills
Honored Contributor
Solution

Re: script problem with read command...

Since I assume your STDIN is set to an input file, you need to have the second read do the input from the terminal. Try-

read answer <&2

This will force read to read from your STDERR (assuming to be the terminal).

HTH

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: script problem with read command...

Hi Michael:

In order to keep the reads "straight" you need to establish another file descriptor. By example:

#!/usr/bin/sh
exec 3<&0
while read LINE
do
echo "${LINE}
read -u3 REPLY
echo "You said: ${REPLY}"
done < filename

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: script problem with read command...

Hi (again) Michael:

As an aside, here's one other trick you will find useful. Instead of testing your 'answer' variable for uppercase and lowercase letters, use 'typeset -l' to translate to lowercase when variable assignment is made:

typeset -l answer
read -u3 answer
if [ "${ansser}" = y ]; then
...

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: script problem with read command...

Another "trick" is to read from /dev/tty. This pseudo device is always your terminal.

e.g. read answer < /dev/tty
If it ain't broke, I can fix that.
Kurt Renner
Frequent Advisor

Re: script problem with read command...

I had a similar question sometime ago. The answers I got were similar, but thought it might be helpful for you to review the responses to my question as well.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xfdac3a7b3682d611abdb0090277a778c,00.html

Thanks,
Kurt Renner
Do it right the first time and you will be ahead in the long run.