What's going on in my scripts?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's going on in my scripts?
# 1  
Old 04-23-2008
What's going on in my scripts?

In my script:

Code:
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
# 2  
Old 04-23-2008
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 ...

Code:
#!/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)

Last edited by era; 04-23-2008 at 08:53 AM.. Reason: Note minor changes
# 3  
Old 04-23-2008
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.
# 4  
Old 04-23-2008
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.
# 5  
Old 04-23-2008
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

2. Shell Programming and Scripting

Calling scripts from with scripts

Hi all, I'm wondering if you could give me some advice. I am new to scripting and am getting rather frustrated that i can get my script to call another script if certain criteria is met, via command line, but I cannot get the same script to work thru the cron jobs. My first script monitors... (8 Replies)
Discussion started by: echoes
8 Replies

3. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

4. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

5. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

6. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

7. UNIX for Dummies Questions & Answers

Profile scripts versus rc scripts....

what is the difference between login and profile scripts versus the rc scripts? (1 Reply)
Discussion started by: rookie22
1 Replies

8. Shell Programming and Scripting

Calling expect scripts from other expect scripts

Hi, First, let me explain the issue I am trying to solve. We have a lot of expect scripts with the duplicated send/expect commands. So, I'd like to be able to extract the duplicated code into the common scripts that can be used by other scripts. Below is my test where I am trying to call... (0 Replies)
Discussion started by: seva
0 Replies

9. UNIX for Dummies Questions & Answers

Help with scripts

script that ask for "enter a file name" and removes that file and asks for confirmation before deletion if executed the output might look as enter the filename you intent to deleted remover file? Y file deleterd I knwo the comand I would use find . -name *.* -ok rm {}\; I guess... can... (1 Reply)
Discussion started by: LiTo
1 Replies
Login or Register to Ask a Question