0% found this document useful (0 votes)
16 views3 pages

How To Use A Shell Script As An Oracle DBA Part 1

Use a shell script as an Oracle DBA: Part I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

How To Use A Shell Script As An Oracle DBA Part 1

Use a shell script as an Oracle DBA: Part I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

How to use a shell script as an Oracle DBA:

Part I
Md. Shamsul Haque, Oracle, PostgreSQL, and SQL Server DBA and Coach
WhatsApp: 01825734821
www.relianceitbd.com

1. Connecting to Oracle Database from Shell Script:

To connect to an Oracle database from a shell script, you can use the sqlplus utility. Make sure
the sqlplus client is installed.

Create a shell script (e.g., connect_db.sh) with the following content:

#!/bin/bash

# Shell script to connect to an Oracle database

# Pre-Req: sqlplus client shall be installed already

# Database details

DB_UserName="your_username"

DB_Password="your_password"

DB_SID="your_SID"

url="(description=(address_list=(address=(protocol=TCP)(host=your_db_host)(port=1521)))(con
nect_data=(service_name=your_service_name)))"

# Check DB connectivity

db_statuscheck() {

echo "$(date): Checking DB connectivity..."

echo "$(date): Trying to connect ${DB_UserName}/${DB_Password}@${DB_SID} ..."

echo "exit" | sqlplus -S ${DB_UserName}/${DB_Password}@${url} | grep -q "Connected to:" >


/dev/null
if [ $? -eq 0 ]; then

echo "$(date): Status: UP. Able to connect."

else

echo "$(date): Status: DOWN. Not able to connect."

echo "$(date): Exiting Script Run..."

exit

fi

# Main function

Main() {

echo "$(date): Starting SQL auto-run script."

db_statuscheck

echo "$(date): SQL auto-run script execution completed."

Main | tee autosql.log

Replace placeholders (your_username, your_password, your_SID, your_db_host, and


your_service_name) with actual values.

Run the script using ./connect_db.sh.

2. Running Queries Inside a Shell Script:

You can execute SQL queries inside a shell script using sqlplus.

Example:

#!/bin/bash

ORACLE_HOME=/u01/app/oracle/product/19.3.0.0/dbhome_1

DBUSER='hr'

DBUSERPASSWORD='hr'

DB='db11g'
var=$($ORACLE_HOME/bin/sqlplus -S ${DBUSER}/${DBUSERPASSWORD}@${DB} << EOD

SELECT * FROM your_table;

EXIT;

EOD

echo "Query result:"

echo "$var"

3. Permissions and Scheduling:

o Ensure that the shell scripts have proper permissions (use chmod +x script_name.sh to
make them executable).

o You can schedule these scripts using cron for automated execution.

Remember to adapt the examples to your specific environment and requirements. Good luck with your
tutorial!

I’ve provided a basic guide, but feel free to ask if you need more details or have any specific questions

Learn more

1 stackoverflow.com2 stackoverflow.com3 learnomate.org4 oracle.com5


stackoverflow.com

You might also like