shell script problem

Hi,

I have a shell script problem. I'm using /bin/ksh and I have a script that reads a number of table names from a file adn then for each table name, it creates the table in Oracle8i via sqlplus,

The main sceleton looks something like:

while read tab_name
do

# BODY

done < $file

Now, if i just print out $tab_name in the BODY of the while loop, then the while loop behaves as expected.

Howeveer, if I include calls to routines that perform calls to 'sqlplus' and create $tab_name in Orcle8i, the while loop is only executed once. Basically, only the first iteration is performed, and only one $tab_name is read from $file.

Any idea why this is happening?

Any help will be greatly appreciated.

S T

Hi,

i test a small script and it works fine. Maybe there's something wrong in the "BODY", can you send the complete loop to me ?

If I understand what you are saying, the code you are adding to the loop somewhere includes a program that is reading from standard-in. Since standard-on is set to $file within the loop, it is reading all of your input from $file.

One rememdy would be something like:
sqlplus < /dev/null
which would at least leave the outer loop intact.

But the fact that the program is unexpectedly reading from standard-in indicates that you have others as well.

You'll probably have to do:

sqlplus -s ...

Also, don't terminate the sql script with a '/'
or 'bye' or anything.

Hi,

I modified my shell script structure and I no longer call 'sqlplus' from within the while loop (I only build an sql file within the loop now).

However the problem specified in my original e-mail still persists.

The 'BODY' section of the while loop (in summary) executes the following commands:

  • /usr/sbin/useradd someuser
  • echo "some text" >> /programs/files/somefile
  • echo "other text" >> /programs/files/otherfile
  • more "somefile" | grep "some seach string"
  • su -u "someuser" -c "some command"

Could the above commands somehow interupt the while loop from
normal execution?

Maybe stdin which is originally set to $file (see previous message)
is being overwritten by one of the command in the 'BODY'. I do not explicitly ovewrite stdin anywhere in the 'BODY' of the while loop.

ref:
while read tab_name
do

   \# BODY 

   done &lt; $file  
  
   \(loop only executes once, even though $file contains
    numerous entries\)

Any help will be greatly appreciated.

Regards,
S. Trpeski

Have you tried running the script with tracing: set -x and see what happens

Did you check that your input file does not contain blank lines: e.g.: the 2nd line?

Thanks for the help/suggestions everyone ...

I have tried running with the -x debug option and the script terminates at the beggining of the second iteration. Namely,

 1. it reads the "while read name" line initially. 
 2. Executes the loop body.  
 3. it reads the "while read name" line for the second time.
 4. stops because it 'thinks' there is no more data in the $file. 

I think this indicates that I have lost my 'stdin' pointer somewhere
in the body of the while loop, but being fairly new with shell scripts, I'm not sure what is happening exactly.

Also, the $file does not contain an empty second line.

Cheers,
Steve T.

It may be possible that stdin is getting
hosed by the line:
more "somefile" | grep "some seach string"
...since the pipe redirects stdout to the
stdin of grep. This is just a guess however
since I have not tested this.
Since you're using ksh, you may want to try
opening a new file descriptor (say 3) for the
loop to read from. Again, just a guess.

If I were you...
And if it taking me a lot of time and causing me a headache I'd use awk reading your $file as an input in order to create a file with *.sql and then execute that file from sqlplus, every line in your input file will have the entire command line you want to execute, I know, this is not a very "polite" shell scripting but if it is an emergency I'd use it.:rolleyes:

Try running the script - as a test - with different lines commented in/out in the BODY each time. See the results then.

Also you may want to check that your KSH is at proper patch level. You never know you could run into a weird bug.

hi,

I resorted to creating one sql file with all table definitions in it. I then manualy load the file via sqlplus.

thanx everyone for your help.