Solved

Run sql statement in Unix script

Posted on 2007-04-03
9
2,923 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle RAC 12c 8 87
Oracle sql query 7 74
Trying connecting to SQL server 2016 from sql developer using Third Party JDBC Drivers, 2 33
update statement in oracle 9 29
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
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.
Via a live example, show how to take different types of Oracle backups using RMAN.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

839 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