views:

157

answers:

2

Given a shell script containing a bunch of SQL statements, is there an option to redirect just the SQL statements to stdout/file?

The structure of the script is something like this:

...

for i in *list*  
do  
  isql *credentials etc* <<EOF > a.out  
  select *about 100 cols*  
from $i + "_TAB"  
go  
EOF  
done

...

Query has been simplified, and is quite complex in reality.

How can I grab the actual statements that are executed when I run this script?

Much appreciate your help.

+1  A: 

The -e (and often with -n) option will echo the input Sybase document for isql although you will get the output as well. If you want to not have the output - selects etc you will need to write a function called isql that will echo the command line to one file and run the isql to another (easier in perl or python)

Mark
Apologies for the delayed reply. Yes, this option would work, but was hoping that I didn't have to code much :)Anyways thanks, Mark!
Beni
+1  A: 

Use a variable to hold your here doc, then you can echo it.

for i in *list*  
do  
    read -r -d '' select <<-EOF
        select *about 100 cols*  
        from $i + "_TAB"  
        go  
        EOF  
    isql *credentials etc* <<< "$select"
    echo "$select" > a.out
done

The hyphen after the << allows you to indent the here doc, but only with actual tabs. Tabs converted to spaces, as is done automatically by some editors, will not work.

Dennis Williamson
Dennis, apologies to you too for the delayed reply.I tried this, but didn't work at all. First, the <code>echo $select</code> gave me a funny output (listing of all files in pwd), and then there were no records returned from the DB (it should; query is correct).
Beni
I'm sorry, the `$select` should have quotes around it since your query probably has asterisks in it. I've edited my answer.
Dennis Williamson