What's going on in my scripts?

In my script:

1  #!/bin/ksh
     2  #To retrieve the ID for lots of the channels
     3  #The id is the _NEWESTEID
     4  #Usage: RetrieveID.ksh <filein> <fileout>
     5  set -x
     6
     7  tmpfile="/tmp/tmpfile"
     8  tmpfile2="/tmp/tmpfile2"
     9
    10  if [ $# -ne 2 ]; then
    11    echo "Usage:"
    12    echo "$0  <filein> <fileout> "
    13    exit 1
    14  fi
    15
    16  while read line
    17  do
    18    CHANNELNAME=`echo $line | awk -F: '{print $3}' `
    19    dbFindChannel -n ${CHANNELNAME} >${tmpfile2}
    20
    21    if [ $? -eq 0 ]
    22    then
    23      NewID=`more ${tmpfile2} | grep DBUS_CHANNEL_NEWESTEID | awk '{print $2}'`
    24    else
    25      NewID="Failed"        #If dbFindChannel failed
    26    fi
    27
    28    echo $line "\t" $NewID >>${tmpfile}
    29  done <$1
    30
    31  mv ${tmpfile} $2
    32  rm -f ${tmpfile2}
    33
    34  exit 0

I use the script like this:
./RetrieveID.ksh filein fileout
everytime as soon as dbFindChannel successed, the script exit. So it can only retrieve the first channel new id, but not all the successful one. Do I miss something here? Why did the script exit the while loop even in the <filein>, there are a lot of channel remains.

Thanks and regards

Does dbFindChannel eat your standard input so it's empty on the next iteration?

Try rearranging the file descriptors.

I'm taking the liberty to fix some fluff while editing this ...

#!/bin/ksh
#To retrieve the ID for lots of the channels
#The id is the _NEWESTEID
#Usage: RetrieveID.ksh <filein> <fileout>
set -x

tmpfile="/tmp/tmpfile"
tmpfile2="/tmp/tmpfile2"

if [ $# -ne 2 ]; then
    echo "Usage:"
    echo "$0  <filein> <fileout> "
    exit 1
fi

exec 3<"$1"
while read line <&3
do
    CHANNELNAME=`echo $line | awk -F: '{print $3}' `

    if dbFindChannel -n ${CHANNELNAME} >${tmpfile2}
    then
        NewID=`awk '/DBUS_CHANNEL_NEWESTEID/ {print $2}' ${tmpfile2}`
    else
        NewID="Failed"        #If dbFindChannel failed
    fi

    echo "$line" "\t" "$NewID" >>${tmpfile}
done  # no <$1
exec 3<&-

mv ${tmpfile} "$2"
rm -f ${tmpfile2}

exit 0

Note the addition of double quotes around variables, the "if" flow, and the lack of Useless Use of More (sic)

Thank you era, again!! It works now.

The if can determine the exit status of the command, I forget this!!! And the powerful AWK...
But could you explain the exec command? It seems strange and unfamiliar to me, How does this modification make my script work? how does this work?If you can show me a path( a link or some comments), I will be very appreciated.

The problem is that you have two things which want to read standard input. Your while loop reads one line at a time, but the dbSomething (or something else in that script) slurps in as much as it can get, the first time it runs.

So we make them read from different file descriptors. That's what the exec does: it connects the input file to file descriptor 3 (instead of 0, standard input; 1 and 2 are standard out and standard error, respectively) and then we modify the while loop to expect its input from that file descriptor. Then at the end we close fd3 when we're done with it, with another exec.

exec, like set, has two different uses. One is to replace the current shell with another process (exec programname); the other is to manipulate the current shell's file descriptors. We use the latter functionality here.

Probably you could redirect dbSomething to read from /dev/null instead -- that would be a simpler change, all right, but I wasn't particularly in the mood to speculate on whether that will work with that particular program or not.

It makes a bit more clear to me. Thank you for your detailed explanation... I can seek for some real and good scripts, and study from the basic.

Thank you again...