Click to See Complete Forum and Search --> : Passing files through a DB2 script?


Linux_cat
08-25-2004, 08:02 AM
Right, i am currently in the process of writing a script that will look into a directory and pass every single text file located in the directory through a DB2 scritping command to automate my testing.

Problem is i dont know how to get the script to
a) read all files in a directory
b) for each file run the DB2 command, one after another
c) not duplicate or use a file more than once.

can someone shed some light on this matter, i am still very much learning bash scripting so any help would be greatly appreciated especially with syntax's.

I am currently doing the first part of the script where i test the directory this is as far as i have got:

#!/bin/bash
echo -e "This script will automate the testing for you, please enter the name of the directory that holds the sql text files:\
c"
read directory
if [ -d directory ]
then
cd $directory; ls -1 *.sql
while read filename
do
echo $filename
done


I thank everyone in advance for any help they can offer.

chrism01
08-25-2004, 08:30 AM
I take it this is related to your prev thread... http://www.justlinux.com/forum/showthread.php?s=&threadid=131895&highlight=db2

Anyway, you can use the same loop as shown in that thread.

Basically where you've got ls -1 *.sql (i your current qn) do this instead (on a new line).


for sql_file in `ls -1 *.sql`
do
db2_cmd $sql_file
done

NB: those are backquotes in the 1st line.
You can either capture any output at the top level (ie catch the output of this ctrl script) or, for each script use this version:
db2_cmd $sql_file 1>$sql_file.log 2>&1

HTH

Linux_cat
08-25-2004, 08:47 AM
Thank you, it is much appreiciated.

bwkaz
08-25-2004, 07:07 PM
chrism01, your bash will break if any files have a space in their names (or a newline). ;)

Just use this instead, it does the exact same thing except that it works with spaces and newlines in filenames:

for sql_file in *.sql The rest of the loop is the same (except it might be a good idea to put double quotes around $sql_file everywhere it appears inside the loop, so the spaces don't trip you up later).