Passing PL/SQL variable value to Shell Varible | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing PL/SQL variable value to Shell Varible

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-29-2007
ganapati's Avatar
ganapati ganapati is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 17 October 2012, 10:54 AM EDT
Location: Mysore
Posts: 148
Thanks: 2
Thanked 0 Times in 0 Posts
Java Passing PL/SQL variable value to Shell Varible

Hi my UNIX Friends,

Im calling some SQL scripts through Unix Shell scripting.
How do I export the value of PL/SQL variable value into a Unix shell script variable?

Also could any one inform me about the 'search' and 'cut' utility of PL/SQL
(like 'grep' and 'cut' in Shell scripting).

Thanks and Regards,
Ganapathi.
Sponsored Links
    #2  
Old 01-29-2007
roopla roopla is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 3 September 2009, 11:46 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
shell_var=`$ORACLE_HOME/bin/sqlplus -S test/passwd@service > ${logfile} 2>&1 << EOF
SET FEED OFF;
SET TERMOUT ON;
SET VERIFY OFF;
SET ECHO OFF;
SET HEAD OFF;
SELECT sysdate from dual FROM DUAL;
EXIT
EOF`

echo $shell_var
Sponsored Links
    #3  
Old 01-30-2007
ganapati's Avatar
ganapati ganapati is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 17 October 2012, 10:54 AM EDT
Location: Mysore
Posts: 148
Thanks: 2
Thanked 0 Times in 0 Posts
Bug It worked, but one more question?

Roopla, Thanks a lot. It worked for me with some fine tuning as below.

shell_var=`sqlplus -s scott/tiger << !
SET HEAD OFF;

SELECT sysdate from dual;
EXIT
!`
echo $shell_var



But still I dont know how to pass multiple values from SQL to Shell script.
Any help in this regard will definitely help all of us to enhance our knowledge.


Once again Many thanks for your sincere help, Roopla.

With Regards,
Ganapati.
    #4  
Old 01-30-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 October 2014, 9:43 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
check this
Sponsored Links
    #5  
Old 01-30-2007
ganapati's Avatar
ganapati ganapati is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 17 October 2012, 10:54 AM EDT
Location: Mysore
Posts: 148
Thanks: 2
Thanked 0 Times in 0 Posts
Great help and many thanks for your guide 'Radoulov'.

Below code is working perfectly to pass multiple values from SQL to Shell script.

printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp where ENO=7021;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"


But still I've one more requirement.....,
What is the case for the same query where if I want assign more values to var1 ?


printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"


Please help me last time.

With Regards,
Ganapati.
Sponsored Links
    #6  
Old 01-30-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
You can try somthing like that (not tested) :

Code:
printf "%s\n" "set pages 0 serverout on feed off" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
"select ENAME from emp;" \
| sqlplus -s scott/tiger \
| { read read var2; read var3; read var4; read var5;
     var1=""
     while read emp
     do
         var1="$var1$emp "
     done
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"


Jean-Pierre.
Sponsored Links
    #7  
Old 01-30-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 October 2014, 9:43 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
Do it all in the database, if possible.
If you _really_ have to mix sql/plsql and shell programming:

- as array in bash:


Code:
$ var1="($(printf "%s \n" "set pages 0 feed off" \
> "select ename from emp;"|sqlplus -s scott/tiger))"; \
> printf "The first element in \$var1 array is: %s, \
> the second: %s and so on ...\n" \
> "${var1[1]}" "${var1[2]}"
The first element in $var1 array is: ALLEN, the second: WARD and so on ...

- as a single variable:


Code:
$ var1="$(printf "%s \n" "set pages 0 feed off" \
> "select ename from emp;"|sqlplus -s scott/tiger)"; \
> echo "Quoted: $var1"; \
> echo "Unquoted:" $var1
Quoted: SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
Unquoted: SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER

I'm sure that it will be better
if you use only one programming language.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing shell variable to awk script Reboot Shell Programming and Scripting 8 02-26-2012 03:29 AM
Passing Variable Parameters (C shell) CKT_newbie88 Shell Programming and Scripting 1 03-07-2011 03:16 PM
Passing a Shell Variable to awk Gussifinknottle UNIX for Dummies Questions & Answers 5 01-27-2011 02:28 AM
passing value to shell variable trynew Shell Programming and Scripting 2 06-24-2002 02:13 PM
Using varible/varible substitution in Perl/sed Search & Replace Breen Shell Programming and Scripting 3 05-08-2002 05:45 AM



All times are GMT -4. The time now is 03:57 PM.