Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

Free Oracle Tips

HTML Text

 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB   


 

 

 


 

 

 
 

Passing variables to a Shell Script 

Expert Oracle Tips by Burleson Consulting

February 17, 2011

 

Question:  Within SQL*Plus is there a way to get this value into a shell script environment variable?

How do I pass a variable to SQL*Plus in a ksh shell script?

Answer:  Passing variables to shell scripts is tricky!  Jon Emmons as a great book "Oracle Shell Scripting" with lots of working examples of passing user defined variables to Oracle in a shell script.  Working scripts exist in the books code download.

Also see passing variables to SQL*Plus.

 I used numbered variables, like &1 and &2, but as long as they are defined and exported in the shell script, they will be resolved in SQL*Plus (unless they contain special characters).  

In cases where a variable contains special characters ($owner), you have to "escape" variables in sqlplus when they contain dollar signs . . .

 If you do it all one line (a requirement in Windows shell scripting), it's easy to pass a ksh shell script variable into sqlplus:

#!/bin/ksh
$min_snap=421
export $min_snap

$max_snap=426
export $max_snap

$ORACLE_HOME/bin/sqlplus -S perfstat/perf$ORACLE_SID@$ORACLE_SID @sppurge $min_snap $max_snap

Here are some working examples using the C shell:

#!/bin/sh
lookup=$1
sqlplus -S system/manager << EOF
SELECT username, account_status, expiry_date
FROM dba_users WHERE lower(username)=lower('$lookup');
exit;
EOF

Shell Variables and Embedded SQL

Earlier in this chapter, we saw how SQL can be embedded in a shell script.  One side benefit to this method is that we can use shell variables to represent values within the embedded SQL.  In the following user_info.sh script we pass a username to the script as an argument, it is transferred to a variable named lookup for better readability and the variable lookup is used in the WHERE clause within the SQL.

#!/bin/sh
lookup=$1
sqlplus -S system/manager << EOF
SELECT username, account_status, expiry_date
FROM dba_users WHERE lower(username)=lower('$lookup');
exit;
EOF

The result is a single script which can take an argument and execute SQL using the value of that argument.  This can be a very efficient way to load data into Oracle tables without the need for a load file or other intermediate step.

Again, for complete examples of passing complex variable to SQL*Plus from  a shall script, get the code download in "Oracle Shell Scripting".

 
 
  Guarantee your Success!

Oracle is the world's most complex, robust and flexible database, considered impossible to master without a mentor.

That's why all BC Oracle trainers are working professionals, experts in Oracle who share their tips and secrets.


 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2012 

All rights reserved.

Oracle © is the registered trademark of Oracle Corporation.


 

  
 

 
 
 
 
Oracle performance tuning software
 
 

 

 
 
 
Oracle performance Tuning 10g reference poster
 
 
 
Oracle training in Linux commands
 
Oracle training Excel
 
Oracle training & performance tuning books