How to redirect SQLPLUS output from /dev/null back to stdout?

I use a script (see below) to place SQLPLUS output into a flat file (see below).
I also use the >/dev/null redirection to suppress an output from SQLPLUS to the stdout.
It works OK.

But now I want to execute another SQLPLUS statement (in the same script file) and I need to send that new output to STDOUT so it could be displayed to the user.

So how can I redirect the output from from /dev/null back to stdout in the same script file?
My simplified script is below.  I want max(mynumber) be displayed by STDOUT.  
Is that possible?  Thank you.

#!/bin/ksh
sqlplus -s /nolog > /dev/null 2>&1 <<EOF
connect myusername/mypassword

set echo off
set feedback off
set linesize 500
set pagesize 0
set sqlprompt ''
set term off
set verify off
set trimspool on

spool /my_directory/my_filename
select * from my_table;
spool off

select max(mynumber) from my_table;

quit
EOF
exit

pavelmedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
you can't redirect from /dev/null to anywhere else.  It's a black hole destination.

You can instead, redirect stdout to something other than /dev/null.

or, simply DON'T redirect stdout at all
0
slightwv (䄆 Netminder) Commented:
To add:

You can add a second sqlplus call for the count and not redirect the output to /dev/null
or
spool the count output to a different file and 'cat' the file outsice the sqlplus call.

something like:
spool /tmp/mycount.txt
select max(mynumber) from my_table;
spool off
quit
EOF

cat /tmp/mycount.txt
rm /tmp/mycount.txt
exit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pavelmedAuthor Commented:
I have to redirect first output because otherwise I can't suppress displaying the output while writing it to the flat file.  The output contains many rows, so I need to suppress it.
You can see my settings on the script.
But then I do need to display an output of the second query.
How can I do that?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

pavelmedAuthor Commented:
Regarding the cat suggestion, I said I need to display the output, but in fact I need to display in in stdout which will then go to the log file that I do not control.  So I cannot use the cat here.
0
pavelmedAuthor Commented:
Let me ask another question: can I redirect the stdout later in the script?
I mean, first I would run the query to display max(mynumber), then I will redirect the stdout to /dev/null.

Can it be done with the same script?  Such as below
#!/bin/ksh
sqlplus -s /nolog <<EOF
connect myusername/mypassword

set echo off
set feedback off
set linesize 500
set pagesize 0
set sqlprompt ''
set term off
set verify off
set trimspool on

select max(mynumber) from my_table;

-- REDIRECT to /dev/null HERE

spool /my_directory/my_filename
select * from my_table;
spool off

quit
EOF
exit
0
slightwv (䄆 Netminder) Commented:
>>I said I need to display the output, but in fact I need to display in in stdout

I don't understand.  cat will display it to stdout.

>>Let me ask another question: can I redirect the stdout later in the script?

No.  not in the same SQLPLUS call.

you can if you do two sqlplus calls (then I'd still go with the 'cat' option over this):
sqlplus >/dev/null <<EOF
select large table
...
EOF

sqlplus <<EOF
select count(*) from table;
EOF
0
sdstuberCommented:
the problem is the "set term off" doesn't work when you are running sql*plus interactively.
it only applies in a script

try something like this...  a 2 line ksh script with all the sql*plus commands in a separate file as in the attached snippet
------------------------------------------------------
#!/bin/ksh
sqlplus -s /nolog @t.sql
------------------------------------------------------


connect myusername/mypassword

set echo off
set feedback off
set linesize 500
set pagesize 0
set sqlprompt ''
set term off
set verify off
set trimspool on

spool ./my_filename
select * from user_objects;
spool off

set term on
select max(created) from user_objects;
exit

Open in new window

0
pavelmedAuthor Commented:
I decided to proceed with the "cat" option by slightwv

Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.