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?
 
slightwv (䄆 Netminder)Connect With a Mentor 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
 
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.