Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return code from sqlplus 4

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
I'm calling sqlplus in a script & I want to tee the output to a logfile like this:

Code:
sqlplus -s my_id/my_pwd <<EOF  | tee -a /tmp/xx
select * from v$instance;
exit
EOF
echo $?

My problem is that the return code ($?) is from the tee command (which always works) & not from the sqlplus (which fails if I enter the wrong id or password). Can anyone suggest how I use the return code from sqlplus?

This works fine if I don't tee the output to a logfile.

Thanks, Chris
 

Try this:
Code:
(sqlplus -s my_id/my_pwd <<EOF
select * from v$instance;
exit
EOF
echo $?
)| tee -a /tmp/xx
[3eyes]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Nice work around LKBrwnDBA!

Another possibility is to do this:

Code:
sqlplus -s my_id/my_pwd <<EOF  >> /tmp/xx
select * from v$instance;
exit
EOF
echo $?
cat /tmp/xx

Instead of using the tee, you can just cat the file after the lines inside a script! (that's if you still want to capture the return code somewhere inside a script!

Regards,
Khalid
 
Will prob use LKBrwnDBA's solution but both work fine.

Thanks, Chris
 
Depends on if you want to be able to use the SQLRC afterwards, if so, then Khalid's soln is the way to go, because with the (commands)-construct, the SQLRC is short-lived (subshell lifespan only)...

Code:
SQLRC='none'
(sqlplus -s my_id/my_pwd <<EOF
select * from v$instance;
exit
EOF
SQLRC=$?
)| tee -a /tmp/xx

echo $SQLRC # will still show none

Code:
SQLRC='none'
sqlplus -s my_id/my_pwd <<EOF  >> /tmp/xx
select * from v$instance;
exit
EOF
SQLRC=$?
cat /tmp/xx

echo $SQLRC # correct RC


HTH,

p5wizard
 
If you want a transient dump of exit status to stdout and not the tee'd file, you can always play with file descriptors.
Code:
((sqlplus -s my_id/my_pwd <<EOF
select * from v$instance;
exit
EOF
echo $? >&3
)| tee -a /tmp/xx 3>&- ) 3>&1

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top