Odd ksh problem

Discussion in 'Programmer Misc' started by Robert Peirce, Feb 29, 2008.

  1. This worked on Uwin, but it is blowing up on my Mac. I just have a
    fragment here.

    Data prints out a colon separated record of every week in our database
    for a stock "$i." I change the colon to <TAB>, extract the first, third
    and fourth fields and pass the last 200 lines on to a while loop.

    I put tee foobar in to be sure the data was actually getting to the
    loop. It is.

    The loop has been compressed to just print the lines coming in. From 0
    to 2 random lines get printed. There is no pattern I have seen. It is
    possible you can't pipe data to a while loop on a Mac. Maybe it isn't
    supposed to work on any machine. Maybe there is something about dual
    core machines that causes the problem.

    I could rewrite the script to use awk. Originally it was an awk script
    that I rewrote to experiment with ksh. However, ksh ought to work if I
    set it up right.

    data $1 |
    tr : '\t' |
    field 1 3 4 |
    tail -200 |
    tee foobar |

    while read -r line
    do print -r -- $line
    done

    --
    Robert B. Peirce, Venetia, PA 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]
     
    Robert Peirce, Feb 29, 2008
    #1
    1. Advertisements

  2. In article <>,
    Robert Peirce <> wrote:

    > This worked on Uwin, but it is blowing up on my Mac. I just have a
    > fragment here.
    >
    > Data prints out a colon separated record of every week in our database
    > for a stock "$i." I change the colon to <TAB>, extract the first, third
    > and fourth fields and pass the last 200 lines on to a while loop.
    >
    > I put tee foobar in to be sure the data was actually getting to the
    > loop. It is.
    >
    > The loop has been compressed to just print the lines coming in. From 0
    > to 2 random lines get printed. There is no pattern I have seen. It is
    > possible you can't pipe data to a while loop on a Mac. Maybe it isn't
    > supposed to work on any machine. Maybe there is something about dual
    > core machines that causes the problem.
    >
    > I could rewrite the script to use awk. Originally it was an awk script
    > that I rewrote to experiment with ksh. However, ksh ought to work if I
    > set it up right.
    >
    > data $1 |
    > tr : '\t' |
    > field 1 3 4 |
    > tail -200 |
    > tee foobar |
    >
    > while read -r line
    > do print -r -- $line
    > done


    If I say

    tail -200 > foobar
    exec 0< foobar
    while . . .

    it works, but isn't stdin always supposed to be open?

    --
    Robert B. Peirce, Venetia, PA 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]
     
    Robert Peirce, Feb 29, 2008
    #2
    1. Advertisements

  3. Robert Peirce

    Bob Harris Guest

    In article <>,
    Robert Peirce <> wrote:

    > This worked on Uwin, but it is blowing up on my Mac. I just have a
    > fragment here.
    >
    > Data prints out a colon separated record of every week in our database
    > for a stock "$i." I change the colon to <TAB>, extract the first, third
    > and fourth fields and pass the last 200 lines on to a while loop.
    >
    > I put tee foobar in to be sure the data was actually getting to the
    > loop. It is.
    >
    > The loop has been compressed to just print the lines coming in. From 0
    > to 2 random lines get printed. There is no pattern I have seen. It is
    > possible you can't pipe data to a while loop on a Mac. Maybe it isn't
    > supposed to work on any machine. Maybe there is something about dual
    > core machines that causes the problem.
    >
    > I could rewrite the script to use awk. Originally it was an awk script
    > that I rewrote to experiment with ksh. However, ksh ought to work if I
    > set it up right.
    >
    > data $1 |
    > tr : '\t' |
    > field 1 3 4 |
    > tail -200 |
    > tee foobar |
    >
    > while read -r line
    > do print -r -- $line
    > done


    why not try

    data $1 | awk -F: '{print $1, $3, $4}' | tail -200
     
    Bob Harris, Mar 1, 2008
    #3
  4. In article <>,
    Bob Harris <-Harris.us> wrote:

    > In article <>,
    > Robert Peirce <> wrote:
    >
    > > data $1 |
    > > tr : '\t' |
    > > field 1 3 4 |
    > > tail -200 |
    > >
    > > while read -r line
    > > do print -r -- $line
    > > done

    >
    > why not try
    >
    > data $1 | awk -F: '{print $1, $3, $4}' | tail -200


    No reason. I just tried to run the script as it sat. I figured I would
    worry about changing it once I got it working. Besides, this change
    doesn't make the while loop work.

    The main question was why can't I pipe information into a ksh while loop
    on my Mac when I can do it on other Unix installations? This is made
    more confusing to me by the fact that the following does work:

    .. . .
    tail -200 > tmp-file

    exec 0< tmp-file
    while read -r line
    .. . .

    From my point of view, stdin is stdin and a pipe should work here.

    --
    Robert B. Peirce, Venetia, PA 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]
     
    Robert Peirce, Mar 1, 2008
    #4
  5. Robert Peirce

    Bob Harris Guest

    In article <>,
    Robert Peirce <> wrote:

    > In article <>,
    > Bob Harris <-Harris.us> wrote:
    >
    > > In article <>,
    > > Robert Peirce <> wrote:
    > >
    > > > data $1 |
    > > > tr : '\t' |
    > > > field 1 3 4 |
    > > > tail -200 |
    > > >
    > > > while read -r line
    > > > do print -r -- $line
    > > > done

    > >
    > > why not try
    > >
    > > data $1 | awk -F: '{print $1, $3, $4}' | tail -200

    >
    > No reason. I just tried to run the script as it sat. I figured I would
    > worry about changing it once I got it working. Besides, this change
    > doesn't make the while loop work.
    >
    > The main question was why can't I pipe information into a ksh while loop
    > on my Mac when I can do it on other Unix installations? This is made
    > more confusing to me by the fact that the following does work:
    >
    > . . .
    > tail -200 > tmp-file
    >
    > exec 0< tmp-file
    > while read -r line
    > . . .
    >
    > From my point of view, stdin is stdin and a pipe should work here.


    Well, why not try a different shell. For example instead of ksh,
    try bash or zsh. The 'while', the 'read' and the stdin
    redirection is a function of the shell. Perhaps it is your shell
    that is having problems.

    If the behavior works with a different shell, then check the
    version of ksh on Mac OS X vs the other Unix systems you use. On
    Tiger I'm seeing:
    version sh (AT&T Labs Research) 1993-12-28 p
    and Leopard is showing:
    version sh (AT&T Research) 1993-12-28 s+

    MacPorts says it has the following 2 versions of ksh
    ksh93 @2007-06-28 shells/ksh93
    pdksh @5.2.14 shells/pdksh

    Bob Harris
     
    Bob Harris, Mar 2, 2008
    #5
  6. In article <>,
    Bob Harris <-Harris.us> wrote:

    > In article <>,
    > Robert Peirce <> wrote:
    >
    > > In article <>,
    > > Bob Harris <-Harris.us> wrote:
    > >
    > > > In article <>,
    > > > Robert Peirce <> wrote:
    > > >
    > > > > data $1 |
    > > > > tr : '\t' |
    > > > > field 1 3 4 |
    > > > > tail -200 |
    > > > >
    > > > > while read -r line
    > > > > do print -r -- $line
    > > > > done
    > > >
    > > > why not try
    > > >
    > > > data $1 | awk -F: '{print $1, $3, $4}' | tail -200

    > >
    > > No reason. I just tried to run the script as it sat. I figured I would
    > > worry about changing it once I got it working. Besides, this change
    > > doesn't make the while loop work.
    > >
    > > The main question was why can't I pipe information into a ksh while loop
    > > on my Mac when I can do it on other Unix installations? This is made
    > > more confusing to me by the fact that the following does work:
    > >
    > > . . .
    > > tail -200 > tmp-file
    > >
    > > exec 0< tmp-file
    > > while read -r line
    > > . . .
    > >
    > > From my point of view, stdin is stdin and a pipe should work here.

    >
    > Well, why not try a different shell. For example instead of ksh,
    > try bash or zsh. The 'while', the 'read' and the stdin
    > redirection is a function of the shell. Perhaps it is your shell
    > that is having problems.


    Thanks, Bob, but that defeats the purpose of trying to figure out why
    this doesn't work when it should. In an earlier version of this script
    I used awk for everything. I switched to ksh because the built-ins
    allowed me to do everything directly in the shell so I didn't have to
    call other programs.

    > If the behavior works with a different shell, then check the
    > version of ksh on Mac OS X vs the other Unix systems you use. On
    > Tiger I'm seeing:
    > version sh (AT&T Labs Research) 1993-12-28 p
    > and Leopard is showing:
    > version sh (AT&T Research) 1993-12-28 s+
    >
    > MacPorts says it has the following 2 versions of ksh
    > ksh93 @2007-06-28 shells/ksh93
    > pdksh @5.2.14 shells/pdksh
    >
    > Bob Harris


    The version I am running at the office is 1993-12-28 q. It is possible
    the pipe-to-while capability was added in q and did not exist in p.

    I found www.macports.org, but I cannot figure out if there is a way just
    to get ksh93 without installing the whole MacPorts package, so I am
    going to do that and try to get there version of ksh93.

    --
    Robert B. Peirce, Venetia, PA 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]
     
    Robert Peirce, Mar 2, 2008
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Dr. Boggis
    Replies:
    1
    Views:
    189
    Dr. Boggis
    Jan 28, 2004
  2. Jeffrey Rolland

    Installing ksh from AT&T

    Jeffrey Rolland, Mar 11, 2006, in forum: Apps
    Replies:
    5
    Views:
    185
    Warren Oates
    Mar 12, 2006
  3. Replies:
    11
    Views:
    238
    Paul Nevai
    May 30, 2007
  4. Tim McNamara

    Odd mail.app problem

    Tim McNamara, Jan 27, 2004, in forum: Communication
    Replies:
    0
    Views:
    139
    Tim McNamara
    Jan 27, 2004
  5. Robert Peirce

    Panther and ksh miss-match

    Robert Peirce, Jan 31, 2004, in forum: Misc
    Replies:
    2
    Views:
    144
    Robert Peirce
    Feb 2, 2004
  6. Robert Peirce

    using cat to get a list to for in sh or in ksh

    Robert Peirce, Feb 6, 2008, in forum: Programmer Misc
    Replies:
    6
    Views:
    464
    Robert Peirce
    Feb 7, 2008
  7. Robert Peirce

    ksh scripting problem

    Robert Peirce, Feb 8, 2008, in forum: Programmer Misc
    Replies:
    3
    Views:
    379
    Robert Peirce
    Feb 8, 2008
  8. Replies:
    5
    Views:
    195
Loading...