misguided
misguided

Reputation: 3799

Running Oracle script from bash

My query basically is when I am trying to run multiple oracle scripts from bash how do I put comments in between the scripts. I have tried using a workaround by selecting a string from dual. But the output formatting isn't very good.

Can anyone please suggest me a better way.

My Code

#!/bin/bash
#Run Script
    echo "-------------------------------"
    echo "***Running Script1***"
    echo "-------------------------------"
sqlplus -S UID/PSW@DB << EOF
whenever sqlerror exit sql.sqlcode;
set echo off 
set heading off
@/my/path/Script1
Select '--------------' from dual;
select '***Running Script1***' from dual;
Select '--------------' from dual;
@/my/path/Script2
exit;
EOF

Output

-------------------------------
***Running Script1***
-------------------------------
SP2-0310: unable to open file "my/path/Script1.sql"

--------------


***Running Script2***    

--------------

SP2-0310: unable to open file "my/path/Script2.sql"

Expected Output

-------------------------------
***Running Script1***
-------------------------------
SP2-0310: unable to open file "my/path/Script1.sql"

--------------
***Running Script2***    
--------------    
SP2-0310: unable to open file "my/path/Script2.sql"

Upvotes: 1

Views: 1316

Answers (3)

Marco Baldelli
Marco Baldelli

Reputation: 3728

Try using the PROMPT command of SQL*Plus:

$ cat tmp.sh
#!/bin/bash

sqlplus -S UID/PSW@DB << EOF
whenever sqlerror exit sql.sqlcode
set echo off
set heading off

prompt =======================
prompt *** Running Script1 ***
prompt =======================
@/my/path/Script1

prompt =======================
prompt *** Running Script2 ***
prompt =======================
@/my/path/Script2

exit
EOF

Output:

$ ./tmp.sh
=======================
*** Running Script1 ***
=======================
SP2-0310: unable to open file "/my/path/Script1.sql"
=======================
*** Running Script2 ***
=======================
SP2-0310: unable to open file "/my/path/Script2.sql"

Upvotes: 3

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

How about

Select '--------------' || chr(10) || '***Running Script1***' || chr(10) || '--------------' from dual;

Upvotes: 2

GregHNZ
GregHNZ

Reputation: 8989

In the past, I've used dbms_output.put_line :

dbms_output.put_line('starting process at: '||to_char(sysdate,'HH24:MI:SS'));

This requires this line first, with your initial "set" statements:

set serveroutput on size 1000000;

And if you're doing stuff in a declare / begin / end block, you might want this:

dbms_output.enable;

Which may change your output.

Upvotes: 2

Related Questions