It is currently Thu, 20 Jan 2022 04:25:45 GMT



 
Author Message
 Read command in bourne shell vs. K shell

Hi.  I am modifying a Korn shell script I wrote into a bourne shell
script.  I used a "while read <file>" loop construct to read through
a file line by line in the original K shell file, and it worked fine.

Now, when I use the following command:

"while read suite dvd table2 test2 type2 num_diff junk < statfile.test
do "

the resulting action is that the first line from the file statfile.test
is read, but the read command never increments beyond the first line
in the file.

What can I do to get this read statement to increment line by line
through the file?

Thanks.

...Lorraine

p.s.  If possible, I would appreciate response by e-mail.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                       Lorraine D. Cooper    
                       sas...@unx.sas.com        
                Open Systems Research & Development
                       SAS Institute Inc.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



 Sat, 15 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell
While stranded on information super highway JimLane wrote:
->In article <DnFwyL....@unx.sas.com>,
->>
->>Hi.  I am modifying a Korn shell script I wrote into a bourne shell
->>script.  I used a "while read <file>" loop construct to read through
->>a file line by line in the original K shell file, and it worked fine.
->>
->>Now, when I use the following command:
->>    
->>"while read suite dvd table2 test2 type2 num_diff junk < statfile.test
->>do "
->>
->>the resulting action is that the first line from the file statfile.test
->>is read, but the read command never increments beyond the first line
->>in the file.
->>
     .
     .
     .
     .
     .

->   Try changing this to:

->     exec < statfile.test
->     while read suite dvd table2 test2 type2 num_diff junk
->     do

     This will also work:

     while read suite dvd table2 test2 type2 num_diff junk
     do

     done  < statfile.test

--
                                      /-------------------\    ^~~~~^
                                      |TECHNOLOGY         |    |    |
                                      |No place for wimps |   o|-OO-|o
                                      |          -Dilbert |--- | () |
                                      \-------------------/    |    |
---------------------------------------------------------------------------
Hemant Shah                      |  I haven't lost my mind,
LIDP, Inc.                       |  it's backed up on tape somewhere.
Voice: (708) 960 0133 Ext: 64    |
Fax: (708) 960 0717              |
E-mail: Personal - s...@xnet.com | Above opinions are mine only. Others can
        Work - sha...@lidp.com   | have their own.
WWW: http://www.xnet.com/~shah   |
--------------------------------------------------------------------------



 Sat, 15 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell
In article <DnFwyL....@unx.sas.com>,

   I believe the problem is that everytime the loop
   comes back to the while statement, it reopens statfile.test.

   Try changing this to:

     exec < statfile.test
     while read suite dvd table2 test2 type2 num_diff junk
     do

   This will of course leave stdin assigned to statfile.test,
   which can have an effect on whatever you are doing later
   in your script.  One way to get stdin back to the terminal
   if you need it is to use a temporary file descriptor to
   save it (kinda like the dup(2) function).

        exec  5<&0
        exec < statfile.test
         while read suite ........
        exec <&5

   There is probably a slicker way of getting stdin back to
   the terminal.  I love to hear of it if anyone knows.

                                               -- JimLane
                                        Univ. of Colorado



 Sat, 15 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell
Don't write

  while read foo < file; do
    ...
  done

That won't work because the command "read foo < file" is run each time
through the loop, re-opening <file> and reading from the beginning (as
you've seen).  Instead write:

  while read foo; do
    ...
  done < file

so that file is opened just once and input is given to the entire loop;
then each iteration of the loop will read the next line.
--
-------------------------------------------------------------------------------
 Paul D. Smith <psm...@baynetworks.com>         Network Management Development
 Senior Software Engineer                                   Bay Networks, Inc.
-----------------------------------------------==<http://www.baynetworks.com/>-
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
     These are my opinions--Bay Networks takes no responsibility for them.



 Sat, 15 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell
move the input redirection to after the 'done'
i.e.
while read var1 ...
do
.
.
done < filename
--
Brian Duane - NCR Corporation
(Formerly AT&T GIS, formerly NCR)
email: brian.du...@australia.ncr.com


 Sun, 16 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell

    JimLane> In article <DnFwyL....@unx.sas.com>, Lorraine Cooper
    JimLane> <sas...@brass.unx.sas.com> wrote:
    >>  Hi.  I am modifying a Korn shell script I wrote into a bourne
    >> shell script.  I used a "while read <file>" loop construct to
    >> read through a file line by line in the original K shell file,
    >> and it worked fine.
    >>
    >> Now, when I use the following command:
    >>
    >> "while read suite dvd table2 test2 type2 num_diff junk <
    >> statfile.test do "
    >>
    >> the resulting action is that the first line from the file
    >> statfile.test is read, but the read command never increments
    >> beyond the first line in the file.
    >>
    >> What can I do to get this read statement to increment line by
    >> line through the file?
    >>
    >> Thanks.
    >>
    >> ...Lorraine
    >>
    >> p.s.  If possible, I would appreciate response by e-mail.
    >>
    >> --

    JimLane>    I believe the problem is that everytime the loop comes
    JimLane> back to the while statement, it reopens statfile.test.

    JimLane>    Try changing this to:

    JimLane> exec < statfile.test
    JimLane> while read suite dvd table2 test2 type2 num_diff junk
    JimLane> do

    JimLane>    This will of course leave stdin assigned to
    JimLane> statfile.test, which can have an effect on whatever you
    JimLane> are doing later in your script.  One way to get stdin
    JimLane> back to the terminal if you need it is to use a temporary
    JimLane> file descriptor to save it (kinda like the dup(2)
    JimLane> function).

    JimLane> exec 5<&0
    JimLane> exec < statfile.test
    JimLane> while read suite
    JimLane>         ........
    JimLane> exec <&5

    JimLane>    There is probably a slicker way of getting stdin back
    JimLane> to the terminal.  I love to hear of it if anyone knows.

    JimLane>                                                -- JimLane
    JimLane> Univ. of Colorado

Others answers are :
        while read aa bb cc
        do
                ......
        done < file

In this case the while is forked, so each variable in the while is local
to the while. When is neccessary to have variable result out of the while,
the sample

    JimLane> exec 5<&0
    JimLane> exec < statfile.test
    JimLane> while read suite
    JimLane>         ........
    JimLane> exec <&5

is better.
--

!----------------------------------------------------------------------------
!                                   V
!                        +---------------------+
!                        | A  L  C  A  T  E  L |
!                        +---------------------+
!                           BUSINESS SYSTEMS
!
! Alain Berenguier              Products & Technology Division
! Research & Development        54, Av. Jean Jaures
! Large Systems Department      92707 Colombes Cedex
!                               Phone  : +33-1-47.85.54.87  
!                               Fax    :       47.85.54.24
!                               E-mail : Alain.Berengu...@col.bsf.alcatel.fr
! ---------------------------------------------------------------------------



 Mon, 17 Aug 1998 03:00:00 GMT   
 Read command in bourne shell vs. K shell
: While stranded on information super highway JimLane wrote:
: ->Lorraine Cooper <sas...@brass.unx.sas.com> wrote:
: ->>Hi.  I am modifying a Korn shell script I wrote into a bourne shell
: ->>script.  I used a "while read <file>" loop construct to read through
: ->>a file line by line in the original K shell file, and it worked fine.
: ->>
: ->>Now, when I use the following command:.........

: ->   Try changing this to:.........

:      This will also work:

:      while read suite dvd table2 test2 type2 num_diff junk
:      do
:      done  < statfile.test

Yeah, but it's not near as much fun as switching around file descriptors...

Back in 1993 or maybe 92 some London bound and bored admin set up a
whole ferris wheel of xterms where the shells were connected each head to
tail via file desciptors in a daisy chain where characters entered in one
xterm would make the whole trip around.  Brilliant albiet tough to justify to
t
 I wish I still had the code.

 poohbear

: --
:                                       /-------------------\    ^~~~~^
:                                       |TECHNOLOGY         |    |    |
:                                       |No place for wimps |   o|-OO-|o
:                                       |          -Dilbert |--- | () |
:                                       \-------------------/    |    |
: ---------------------------------------------------------------------------
: Hemant Shah                      |  I haven't lost my mind,
: LIDP, Inc.                       |  it's backed up on tape somewhere.
: Voice: (708) 960 0133 Ext: 64    |
: Fax: (708) 960 0717              |
: E-mail: Personal - s...@xnet.com | Above opinions are mine only. Others can
:         Work - sha...@lidp.com   | have their own.
: WWW: http://www.xnet.com/~shah   |
: --------------------------------------------------------------------------



 Wed, 19 Aug 1998 03:00:00 GMT   
 
   [ 7 post ] 

Similar Threads

1. Bourne Shell vs C-Shell

2. Bourne shell read command

3. Problem with read command in Bourne shell.

4. Q about Bourne shell "read" command

5. Bourne Shell: read command

6. Light Speed Bourne Shell! (was: Bourne shell tricks)

7. Bourne Shell compatible shells (was: Request: which shells)

8. getting environment variables from c shell using Bourne shell

9. Changing from the Bourne shell to C-shell


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software