Running a select script through UNIX and sending output to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running a select script through UNIX and sending output to file
# 8  
Old 04-13-2011
@ Peasant - Tried that, although it gives the same result as some of my previous attempts. A file is produced and the content is:

Code:
SQL>   @scrambler_SELECT_UPDATE_V0.4.sql
 93    EOF
  94   

In fairness I haven't posted the rest of the original script, so here it is...

Code:

#!/bin/sh
 #
 # some text

 
  cd <some dir> 

 
  export  ORACLE_HOME
 
  X= 'sqlplus -r u/p@sid <<selectupdate
 spool <some dir>/selectupdateX.txt
    @<script>.sql;
  selectupdate'
 
  #echo $X >  selectupdateX.txt

@Corona688 - I'll make a new script and test that to see what happens, hopefully it'll give the result I'm after and I can build on that.

Thanks for the help, appreciated.

Gary

*Thinking about it, guess the CD and Oracle_Home bits at the start are probably cocking it up. Please excuse the newb Smilie
# 9  
Old 04-13-2011
Quote:
Originally Posted by dbchud
*Thinking about it, guess the CD and Oracle_Home bits at the start are probably cocking it up.
I don't think the cd will stop it from working.

The export won't hurt it, but I don't think it's doing what you wanted either. If ORACLE_HOME wasn't set to anything, exporting it won't figure out the right value for you. Exporting a blank ORACLE_HOME variable won't actually help Oracle find its home. Smilie

Instead do:
Code:
ORACLE_HOME=/correct/path/to/oracle/stuff
export ORACLE_HOME

Also: You're still putting that in a variable for no benefit. How about using the code directly, instead of cramming it into a variable?

You also haven't moved the last 'selectupdate' to the beginning of the line. It absolutely HAS to be at the beginning of the line, no tabs, no spaces, no nothing.

Code:
cd <wherever>
ORACLE_HOME=/path/to/oracle/stuff/
export ORACLE_HOME

sqlplus -r u/p@sid <<selectupdate
spool <some dir>/selectupdateX.txt
@<script>.sql;
selectupdate

This User Gave Thanks to Corona688 For This Post:
# 10  
Old 04-13-2011
Okay thanks for the feedback. I'm off now but will be picking this up first thing tomorrow morning.

Looking forward to nailing this... !! Smilie
# 11  
Old 04-13-2011
Try this :
Code:
(
sqlplus /nolog <<EOF
conn u/p@$SID
@sql
EOF ) |  tee /path/to/output.txt

Feel free either to use set <options> to remove stuff you don't need, or awk/grep/sed pipe before tee.
# 12  
Old 04-14-2011
@Corona688 - I created a new script, the content is:

Code:
#!/bin/sh

ORACLE_HOME=/apps/oracle/10.2.0.4
export ORACLE_HOME
 
sqlplus u/p@sid <<selectupdate
spool selectupdateX.txt
@<script>.sql;
selectupdate

It created a file called selectupdateX.txt, and the contents are:

Code:
SQL> @<script>.sql;
 93

I did try adding a -r after sqlplus, however when executing the script the sqlplus help popped up and nothing else happened (no file).

I'll keep playing with this, but do you have any more ideas?

@Peasant - I tried your script too:

Code:
#!/bin/sh

ORACLE_HOME=/apps/oracle/10.2.0.4
export ORACLE_HOME
 
(
sqlplus /nolog <<EOF
conn TMH7ESD0_TM_ONLINE/TMH7ESD0_TM_ONLINE@TMH3ESD0
@scrambler_SELECT_UPDATE_V0.4.sql;
EOF
) tee selectupdateX.txt

This generated the following error:

Code:
-bash: <scriptname>.sh: line 11: syntax error near unexpected token `tee'
-bash: <scriptname>.sh: line 11: `) tee selectupdateX.txt'

I haven't attempted to figure out the errors yet.

I'm going to keep trying with both versions. Smilie

Not sure I'll be able to put bash scripting as a skill on my CV just yet Smilie

Cheers,
g

---------- Post updated at 09:14 AM ---------- Previous update was at 03:34 AM ----------

There's something "wrong" with the SQL that I'm running within <script.sql>. Basically the SQL generates a whole load of UPDATE statements through CASE statements.

When I do something like this...

Code:
sqlplus u/p@sid <<selectupdate
spool selectupdateX.txt
select id from <table>;
selectupdate

...then the selectupdateX.txt file is generated with a list of ids!!

When I do something like this...

Code:
sqlplus u/p@sid <<selectupdate
 spool selectupdateX.txt
 select case when ... blah blah end from <table> where <something>;
 selectupdate

...then the selectupdateX.txt file is generated with nothing other than some sqlplus stuff, no UPDATE statements at all.

The issue I have looks like it's because of something dodgy happening with the SQL, and not the UNIX script itself... but maybe I'm wrong... Smilie

---------- Post updated at 09:31 AM ---------- Previous update was at 09:14 AM ----------

Got it, and I feel pretty stupid. I failed to add a semi colon to the end of my massive select statement, so... that's the reason it wasn't doing what I hoped.

Thanks again for everyone's help, appreciated.
# 13  
Old 04-15-2011
You are not pasting it right ...

This works fine.
Code:
(
sqlplus /nolog <<EOF
conn user/pass@$ORACLE_SID
@test.sql
EOF
) | grep '^update' | tee out.sql

test.sql i out.sql content ..
Code:
test.sql :
select 'update table set value=''1'' where value2=''2'';' from dual;
select 'update table set value=''5'' where value2=''7'';' from dual;
out.sql :
update table set value='1' where value2='2';
update table set value='5' where value2='7';

Ofcourse you can use various SET options to avoid pipe grep.
# 14  
Old 04-20-2011
Resolved, cheers everyone
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select command going to infinite loop after running the script

cd /opt/et/WAS/apps/8.0 find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null | awk -F '' '{print $4}'|awk '!x++' | cat>/home/cbadmin/file1.txt cd /home/cbadmin/ PS3='Please enter a number from list of applications==>:' select... (3 Replies)
Discussion started by: bhas85
3 Replies

2. Shell Programming and Scripting

How to print the output of a select query using shell script?

HI, I want to connect to database and fetch the count from a table. The sql query is as below : select count(*) from table_test where test_column='read'; How can I print the output of this statement using shell script. Thanks in advance. (4 Replies)
Discussion started by: confused_info
4 Replies

3. Shell Programming and Scripting

How to process select list of files and output to the same file?

Hi, I've a list of files ac_info.tps, subscription_array.tps, .......and many other files one of the file, bin_range_list.tps has the following content CREATE OR REPLACE TYPE "BIN_RANGE_LIST" AS TABLE OF BIN_RANGE_ELEM; / grant execute on... (4 Replies)
Discussion started by: jediwannabe
4 Replies

4. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

5. AIX

Sending script output as email

Hi i have a script which executes daily through cron. The output of the script is appended to a log file everyday It also emails me the output of the logfile as we have the mailx command in the script The below is my requirement : Normally When I get the email it sends the entire content... (3 Replies)
Discussion started by: newtoaixos
3 Replies

6. Shell Programming and Scripting

output file of the shell script running through crontab is deleting automatical daily.

Dear Friends, I am working on IBM AIX. I have written one script and kept in the crontab as to run daily at 11:38 AM. and the output of the script to be appended to the file generated with the month name. but my file deleting daily and the new file is creating with the output of the shell... (2 Replies)
Discussion started by: innamuri.ravi
2 Replies

7. Shell Programming and Scripting

Running a shell script in cron...email not sending - help??

I am pretty new to Unix shell scripting, but wondered if anyone could help (in layman's terms if possible!!) :) I have a shell script which ultimately sends an alert to an email address if part of a batch of programs fails. Here's the script that sends the email: Script: 6check.csh... (8 Replies)
Discussion started by: tjhorwood
8 Replies

8. Shell Programming and Scripting

FTP script for sending a file from one unix directory to another unix server director

Hi, My local server is :/usr/abcd/ Remote server is :/Usr/host/test/ I want to send files from local unix directory(All files starting with O_999) to remote host unix directory. Can any body give me the Unix Shell script to do this. One more doubt: Shall we need to change the file... (1 Reply)
Discussion started by: raja_1234
1 Replies

9. Windows & DOS: Issues & Discussions

Want to use the output of Select statement in Unix script

Hi, I have a UNIX script which calls SQL Select statement: Now i want to use the output of that select statement within my UNIX script so as to call different shell script depending upon the output of the select statement. Can anyone help me in this regard. TIA Akhil Goel (4 Replies)
Discussion started by: akhilgoel9
4 Replies

10. Shell Programming and Scripting

Sending mail from Unix - For html output

I have automated some checks - and I want to send a email when there is an issue. This is fine and I can send the email. However - I want to generate the email in html format so I can highlight any issues to a reader.. ie. If there is a disk space issue - then the email will highlight the... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question