Solved

Run sql statement in Unix script

Posted on 2007-04-03
9
2,913 Views
Last Modified: 2013-12-21
I am editing a Unix script that logs into the database and executes a sql script in a certain directory. Rather than execute this sql script I want to be able to run sql statement within the unix script. How do I do this.
This is the line in the Unix script:
su - oracle -c "export ORACLE_SID=${db}; sqlplus '/as sysdba' @${PWD}/audit_loc.sql" > ${PWD}/audit_loc.txt

The script is audit_loc.sql

I would like to execute the sql statement within Unix rather than using this: @${PWD}/audit_loc.sql

The contents of the script is the following:
set heading off;
show parameter audit_file_dest;
exit;

0
Comment
Question by:sikyala
  • 6
  • 3
9 Comments
 
LVL 7

Expert Comment

by:nixfreak
ID: 18845021
Not sure but try:
/bin/echo "set heading off;\nshow parameter audit_file_dest;\nexit;" | su - oracle -c "export ORACLE_SID=${db}; sqlplus '/as sysdba' @/dev/stdin" > ${PWD}/audit_loc.txt
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18845044
Small correction, please try:

/bin/echo "set heading off;\nshow parameter audit_file_dest;\nexit;\n" | su - oracle -c "export ORACLE_SID=${db}; sqlplus '/as sysdba' @/dev/stdin" > ${PWD}/audit_loc.txt
0
 

Author Comment

by:sikyala
ID: 18846363
I tried it and it didn't work. what does @/dev/stdin do?
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18847353
Hi sikyala,

Sorry, please try this modification:

su - oracle -c "export ORACLE_SID=${db}; /bin/echo "set heading off;\nshow parameter audit_file_dest;\nexit;\n" | sqlplus '/as sysdba' @/dev/stdin" > ${PWD}/audit_loc.txt
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:nixfreak
ID: 18847406
/dev/stdin can be used with commands that are not designed to take standard input (stdin).

If it still doesn't work perhaps you can also try replacing the single quotes with double quotes surrounding "/as sysdba".
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18847433
Also I realized that "\n" is not required after exit.
0
 

Author Comment

by:sikyala
ID: 18848132
I tried and got the following:

./audit.bash: nshow: command not found
./audit.bash: nexit | sqlplus '/as sysdba' @/dev/stdin: No such file or directory
0
 
LVL 7

Accepted Solution

by:
nixfreak earned 500 total points
ID: 18848730
Ok,
I think the double quotes were causing the problem. So, try:

su - oracle -c "export ORACLE_SID=${db}; /bin/echo 'set heading off;\nshow parameter audit_file_dest;\nexit;' | sqlplus '/as sysdba' @/dev/stdin" > ${PWD}/audit_loc.txt       
0
 

Author Comment

by:sikyala
ID: 18851294
It worked thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now