1

I am trying to store the value of sql query output in a variable using shell script.

size=`${PATH_TO_CLIENT}sqlplus $IMPUSER/$IMPPWD@$ENDPOINT<< EOF
select owner, sum(bytes)/1024/1024/1024 Size_GB from dba_segments where owner = 'XXXX' group by owner;
exit;
EOF`
echo "Total data is ${size}"

The output I am getting is

**Total data is**
SQL*Plus: Release 21.0.0.0.0 - Production on Fri May 14 11:06:42 2021
Version 21.1.0.0.0

Copyright (c) 1982, 2020, Oracle.  All rights reserved.

Last Successful login time: Fri May 14 2021 11:01:02 -04:00

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.8.0.0.0

SQL>
OWNER
--------------------------------------------------------------------------------
   SIZE_GB
----------
XXXXXXX
12.2345


SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.8.0.0.0

Inside the variable full connection string and sql query output all are getting stored. I just want to get value like $size=12.2345 Please tell me how to get that

1
  • you should remove ${PATH_TO_CLIENT} which precedes sqlplus command by properly setting ORACLE_HOME variable. Commented May 14, 2021 at 16:44

3 Answers 3

3

The size value might be assigned to the current variable through use of the following code block

size=$(sqlplus -S /nolog << EOF
 conn $IMPUSER/$IMPPWD@$ENDPOINT
 whenever sqlerror exit sql.sqlcode
 SET PAGES 0
 SELECT SUM(bytes)/1024/1024/1024 FROM dba_segments WHERE owner = 'XXXX';
EOF
)
echo "Total data is "$size

where

  • keeping owner column and group by clause are redundant as returning only one column value for a single schema
  • no need to alias the calculated value as not needed for the returning result while hiding the column title through use of SET PAGES 0 command
  • using direct connection is not safe, but use sqlplus -S /nolog before schema connection in order to hide the password while listed by anbody through ps command.
Sign up to request clarification or add additional context in comments.

Comments

3

You can use this:

size=`${PATH_TO_CLIENT}sqlplus -s $IMPUSER/$IMPPWD@$ENDPOINT <<EOF
set echo off
set feedback off
set heading off
set pages 0
select sum(bytes)/1024/1024/1024 Size_GB from dba_segments where owner = 'SYS';
exit;
EOF`
echo "Total data is ${size}"

2 Comments

this worked but the value was coming in newline after the total data statement Total data is = 11.2105713
Just add one more line after set heading off "set pages 0". I have updated the answer above.
1

If the output is consistent with newlines, you could use:

size=`${PATH_TO_CLIENT}sqlplus $IMPUSER/$IMPPWD@$ENDPOINT<< EOF | sed -n '/^\s*SIZE_GB$/{n;n;n;p}'
select owner, sum(bytes)/1024/1024/1024 Size_GB from dba_segments where owner = 'XXXX' group by owner;
exit;
EOF`

It will return the third line after line which contains 'SIZE_GB'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.