Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using read within a loop that parses a file line by line

4 views
Skip to first unread message

David

unread,
Sep 17, 2003, 8:32:14 PM9/17/03
to
This bit of code is supposed to process a list of files line by line,
and prompt you for a revision number each time. What actually happens
is that the while read LINE ; do ; done < $FILELIST somehow interferes
with read REV command and the result is an infinite loop. You're
never actually prompted for any input, and REV remains an empty
string.

Can someone suggest how to fix this?

while read LINE
do
cfg info $LINE | less
echo "Revision number?"
read REV
rcsdiff -r${REV} $LINE | tee -a $TEMPFILE
echo "\nWrite difference to file (y/n)?"
read OK
if [ $OK = y ]
then
cat $TEMPFILE >> $OUTFILE
echo "Diff written to $OUTFILE"
break
fi
rm -f $TEMPFILE
done
done < $INFILE

Chris F.A. Johnson

unread,
Sep 17, 2003, 10:08:26 PM9/17/03
to
On Thu, 18 Sep 2003 at 00:32 GMT, David wrote:
> This bit of code is supposed to process a list of files line by line,
> and prompt you for a revision number each time. What actually happens
> is that the while read LINE ; do ; done < $FILELIST somehow interferes
> with read REV command and the result is an infinite loop. You're
> never actually prompted for any input, and REV remains an empty
> string.
>
> Can someone suggest how to fix this?

You have redirected stdin, so read gets its input from the
file. To get information from the keyboard, you need to redirect
the input:

read REV < /dev/tty

> while read LINE
> do
> cfg info $LINE | less
> echo "Revision number?"
> read REV
> rcsdiff -r${REV} $LINE | tee -a $TEMPFILE
> echo "\nWrite difference to file (y/n)?"
> read OK
> if [ $OK = y ]
> then
> cat $TEMPFILE >> $OUTFILE
> echo "Diff written to $OUTFILE"
> break
> fi
> rm -f $TEMPFILE
> done
> done < $INFILE


--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

laura fairhead

unread,
Sep 18, 2003, 8:29:18 PM9/18/03
to
On 17 Sep 2003 17:32:14 -0700, dav...@hushmail.com (David) wrote:

>This bit of code is supposed to process a list of files line by line,
>and prompt you for a revision number each time. What actually happens
>is that the while read LINE ; do ; done < $FILELIST somehow interferes
>with read REV command and the result is an infinite loop. You're
>never actually prompted for any input, and REV remains an empty
>string.

When you do the redirection on a command like while/do/done it affects
all the code executed within, while/do/done is regarded as one whole
block rather like { .... }

>
>Can someone suggest how to fix this?

You could set up file descriptor 3 to point to the stdin before
redirecting it to the file so that inside the while/do/done block
stdin (file descriptor 0) is pointing to the file and file descriptor
3 is pointing to wherever stdin was pointing to before

>
>while read LINE
>do
> cfg info $LINE | less
> echo "Revision number?"

> read REV

read REV <&3


> rcsdiff -r${REV} $LINE | tee -a $TEMPFILE
> echo "\nWrite difference to file (y/n)?"
> read OK

read OK <&3

> if [ $OK = y ]

This line can crash on unexpected user input to stop that
irregardless of what they type the variable should be quote
protected;

if [ "$OK" = y ]

> then
> cat $TEMPFILE >> $OUTFILE
> echo "Diff written to $OUTFILE"
> break
> fi
> rm -f $TEMPFILE
> done

>done < $INFILE

done 3<&1 <$FILENAME

seeyafrom
l

--
echo alru_aa...@ittnreen.tocm |sed 's/\(.\)\(.\)/\2\1/g'

laura fairhead

unread,
Sep 18, 2003, 8:29:41 PM9/18/03
to
On 18 Sep 2003 02:08:26 GMT, "Chris F.A. Johnson" <c.f.a....@rogers.com> wrote:

>On Thu, 18 Sep 2003 at 00:32 GMT, David wrote:
>> This bit of code is supposed to process a list of files line by line,
>> and prompt you for a revision number each time. What actually happens
>> is that the while read LINE ; do ; done < $FILELIST somehow interferes
>> with read REV command and the result is an infinite loop. You're
>> never actually prompted for any input, and REV remains an empty
>> string.
>>
>> Can someone suggest how to fix this?
>
> You have redirected stdin, so read gets its input from the
> file. To get information from the keyboard, you need to redirect
> the input:
>

>read REV < /dev/tty


Not all systems support /dev/tty. Also stdin in the program doesn't
have to point to the process terminal (eg; if not redirected from a file
then this fails if run from a socket)


byefornow

j...@invalid.address

unread,
Sep 18, 2003, 11:08:32 PM9/18/03
to
run_signature_sc...@INVALID.com (laura fairhead) writes:

That's true, but unless it's run as a daemon somehow, it probably does
have a controlling terminal, and /dev/tty is the best bet for getting
at that, whether stdin has been redirected or not.

Joe

0 new messages