ksh scripting problem

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

  1. Does anybody have any idea why this works:

    IFS=' '
    exec 0< "db/s.d"
    while read line
    do
    set $line
    if (($3>0)) && ((${15}<16))
    then

    And this doesn't?

    exec 0< "s.p"
    while read line
    do
    set $line
    ((++wkcnt))
    if (($1>0)) [>0: arithmetic syntax error]
    then

    The line is getting read correctly as shown if I do a print $1 after the
    set.

    Actually, since the line in the second case only holds one field,
    it should be possible to get away without doing a set, but I was trying
    to match the other code.

    The first few lines of s.p look like

    16.1299990
    16.7199990
    16.9800000
    16.0000000
    16.9900000
     
    Robert Peirce, Feb 8, 2008
    #1
    1. Advertisements

  2. Abject apologies for rank stupidity. Each line in the s.p file ends
    with \r\n instead of just \n. It is necessary to replace "set $line"
    with something like p=$(print $line | tr -d '\r'). Then ((p>0)) works
    fine.

    The \r crap is a holdover from Windows.
     
    Robert Peirce, Feb 8, 2008
    #2
    1. Advertisements

  3. Okay. Here's a real puzzle and I don't think it has anything to do with
    \r.

    This doesn't work. It hangs at the if statement, which doesn't execute.
    p=$(echo $line | tr -d '\r')
    ((++wkcnt))
    if ((p>0))

    This does work
    p=$(echo $line | tr -d '\r')
    ((++wkcnt))
    #
    if ((p>0))

    As does this
    ((++wkcnt))
    p=$(echo $line | tr -d '\r')
    if ((p>0))

    You need something after ((++wkcnt)) for if to work and that makes no
    sense to me at all.
     
    Robert Peirce, Feb 8, 2008
    #3
  4. I was advised to use the ksh built-in read line to read in a really huge
    file. Well, it turns out, probably because ksh is an interpreter, that
    read line, at least as demonstrated by the following script

    #!/bin/ksh
    exec 0< "db/s.p"
    while read line
    do
    print $line >> /dev/null [echo is actually a hair faster]
    done

    takes way, way longer than cat db/s.p. Consequently, unless someone can
    give me a very fast way to use read line, I am going to stick with my
    kludge of splitting s.p into 50,000 line pieces and processing them in
    order.
     
    Robert Peirce, Feb 8, 2008
    #4
    1. Advertisements

Ask a Question

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.