I need to get total number of rows in a table by using select count(*) from table and need to assign into a variable in shell script. I used the following script, it does return the number of rows but also with oracle headers, Please help me to eliminate this headers.
Shell Script
#!/bin/sh
count=`sqlplus $username/$password@$tnsname << EOF
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
select count(*) from table;
exit;
EOF`
echo "value is "$count
O/P:
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Dec 22 05:01:33 2011 Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options
SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> 525935 SQL>
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options
---------- Post updated at 06:28 AM ---------- Previous update was at 06:10 AM ----------
Well, that had me flummoxed for a while. There I was about to extol the virtues of the ksh command select in building menus and it's an Oracle question really.
I think what you need to add is the -S flag to your sqlplus command.
Hi,
I got the number of records, but when i tried to the count variable in while loop, i have received the below error,
line 23: syntax error near unexpected token `done'
line 23: `done'
#!/bin/sh
outputvar=`sqlplus -S $username/$password@$tnsname << EOF
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
select count(*) from table;
exit;
EOF`
while [ $outputvar -gt 0 ]
do
echo "value is larger"
done
... so you strip out any white space so you can confidently work with the value rather than hoping that the string returned. Sometimes the tests can be a bit picky depending how you phrase them and your distribution.
#!/bin/sh
outputvar=`sqlplus -S $username/$password@$tnsname << EOF
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
select count(*) from table;
exit;
EOF
`
echo "value is "$outputvar
if [ $outputvar -gt 0 ]; then
echo "value is larger"
else
echo "Not larger.."
fi
O/p
value is 52365
test.sh: line 25: syntax error near unexpected token `fi'
test.sh: line 25: `fi'
I have got the same error when i tried with while loop as well.
++ sqlplus -S $'dev/dev\orcl'
' outputvar=' 52365
+ echo 'value is ' $'52365\r\r'
value is 52365
test.sh: line 25: syntax error near unexpected token `fi'
test.sh: line 25: `fi'
Still the same error. Please find the modified code and o/p
#!/bin/sh
outputvar=`sqlplus -S $username/$password@$tnsname << EOF
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
select count(*) from table;
exit;
EOF
`
outputvar=`echo $outputvar | sed 's/\r//g' `
echo "value is "$outputvar
if [ $outputvar -gt 0 ]; then
echo "value is larger"
else
echo "Not larger.."
fi
O/P
' outputvar=' 52365
++ echo $'52365\r'
++ sed 's/\r//g'
+ outputvar=$'52365\r'
' echo 'value is 52365
value is 52365
test.sh: line 26: syntax error near unexpected token `fi'
test.sh: line 26: `fi'