Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Apr 2000 14:22:35 -0700
From:      Alfred Perlstein <bright@wintelcom.net>
To:        Greg Pavelcak <gpav@som.umass.edu>
Cc:        Doug Barton <Doug@gorean.org>, freebsd-questions@FreeBSD.ORG
Subject:   Re: Making sh script pause for input
Message-ID:  <20000425142235.H9754@fw.wintelcom.net>
In-Reply-To: <Pine.OSF.4.10.10004251640440.13660-100000@yolen.oit.umass.edu>; from gpav@som.umass.edu on Tue, Apr 25, 2000 at 04:43:15PM -0400
References:  <Pine.BSF.4.21.0004251258300.38506-100000@dt051n0b.san.rr.com> <Pine.OSF.4.10.10004251640440.13660-100000@yolen.oit.umass.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
* Greg Pavelcak <gpav@som.umass.edu> [000425 14:18] wrote:
> On Tue, 25 Apr 2000, Doug Barton wrote:
> 
> > On Tue, 25 Apr 2000, Alfred Perlstein wrote:
> > 
> > > * Doug Barton <Doug@gorean.org> [000425 11:01] wrote:
> > > > Greg Pavelcak wrote:
> > > > > 
> > > > > This is driving me nuts. I want a script that prompts with a
> > > > > student's name and then waits for input regarding that student
> > > > > then moves on. I've tried using xargs and a script like this:
> > > > 
> > > > 	The bad news, you can't do that with sh because once you tell it to
> > > > take its input from a file that's where it's going to take all of its
> > > > input from. The good news, this is a really easy perl script, and this
> > > > kind of processing is one of the things perl is really good for. 
> > > 
> > > Actually... :)
> > > 
> > > http://www.complete.org/mailinglists/archives/aclug-l-199811/msg00018.html
> > > 
> > > explains some really nifty things you can do with sh and filehandles.
> > 
> > 	None of which apply to the original poster's exmple. He wants to
> > read from the real stdin while inside a loop which is already reading its
> > stdin from a file. If you can do what the author asked for in sh, I'd love
> > to see it. 
> > 
> Just for the record, my original script,
> 
> 
> #!/bin/sh
> 
> while read LOGNUM CLASS LNAME FNAME GPA MAJOR ;
> do 
> grep -iqe "$FNAME \$ $LNAME" appstatus
> if [ $? -eq 1 ]; then
>  	echo $FNAME $LNAME ;
> 	echo -n "Status? " ;
> 	read STATUS
> echo "$LOGNUM \$ $CLASS \$ $FNAME \$ $LNAME \$ $GPA \$ $MAJOR \$ $STATUS" >> appstatus ;
> fi
> done < contractsS00
> 
> was a failure while
> 
> 
> #!/bin/sh
> 
> while read LOGNUM CLASS LNAME FNAME GPA MAJOR ;
> do 
> grep -iqe "$FNAME \$ $LNAME" appstatus
> if [ $? -eq 1 ]; then
>  	echo $FNAME $LNAME ;
> 	echo -n "Status? " ;
> 	read STATUS < /dev/tty
>                     ^^^^^^^^^^
> echo "$LOGNUM \$ $CLASS \$ $FNAME \$ $LNAME \$ $GPA \$ $MAJOR \$ $STATUS" >> appstatus ;
> fi
> done < contractsS00
> 
> was a complete success. I would be glad to credit the kind person
> who sent this to me, but perhaps he prefers to remain an
> anonymous sh whiz.

Actually that's a bad way to do it, simple reason, you can't run the
script without a tty, so now if you run the script like this:

sh myscript.sh < data_for_read_status.txt

It won't work, see my later example for something that should work.

FYI, the revelant info from that URL I gave you guys
was this:

    1b. Reading Files

    In the csh, all you've got is $<, which reads a line from your tty.  What
    if you've redirected stdin?  Tough noogies, you still get your tty, which 
    you really can't redirect.  Now, the read statement 
    in the Bourne shell allows you to read from stdin, which catches
    redirection.  It also means that you can do things like this:
    
        exec 3<file1
        exec 4<file2
    
    Now you can read from fd 3 and get lines from file1, or from file2 through
    fd 4.   In modern, Bourne-like shells, this suffices: 
    
        read some_var 0<&3
        read another_var 0<&4

-- 
-Alfred Perlstein - [bright@wintelcom.net|alfred@freebsd.org]
"I have the heart of a child; I keep it in a jar on my desk."


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000425142235.H9754>