- 11:17 AM - 10 comments
Connecting to oracle database from unix shell script
How to connect to database through Unix Shell Script
In this post we will how to connect Oracle database through unix shell scripting
Connecting to oracle database and accessing data from shell script
We will get sysdate as output with Oracle banner
Output of above code
To remove oracle banner from the output connect the database in silent mode (use -s option)
Connect to a database via UNIX Shell Script in silent mode
$ cat sqlconnect.sh
#!/bin/bash
sqlplus -s / << EOFSQL
select sysdate from dual;
exit;
EOFSQL
$ ./sqlconnect.sh
SYSDATE
---------
22-AUG-09
$
Connect to a Database in Shell Programming and storing the output in UNIX variable
Use back ticks to do the same as
To get access two columns data and storing result in Unix variables
How to get data from a database table into UNIX file using shell script
Using a Shell Script to connect to sql database and execute db query from a unix shell script
Startup and shutdown Oracle Database with UNIX shell script
To startup oracle database from UNIX shell script
To shutdown oracle database from unix shell script
UNIX shell script to run SQL files as
To pass unix variable from shell to Oracle database.
To Call Shell script from SQL use HOST keyword
HOST /home/scripter/scripts/myshell.sh
Please give your valuable doubts or feedback via comments
In this post we will how to connect Oracle database through unix shell scripting
Connecting to oracle database and accessing data from shell script
$ cat sqlconnect.sh
#!/bin/bash
sqlplus / << EOFSQL
select sysdate from dual;
exit;
EOFSQL
$
We will get sysdate as output with Oracle banner
Output of above code
$ ./sqlconnect.sh
SQL*Plus: Release 10.2.0.1.0 - Production on Sat Aug 22 10:52:26 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL>
SYSDATE
---------
22-AUG-09
SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
$
To remove oracle banner from the output connect the database in silent mode (use -s option)
Connect to a database via UNIX Shell Script in silent mode
$ cat sqlconnect.sh
#!/bin/bash
sqlplus -s / << EOFSQL
select sysdate from dual;
exit;
EOFSQL
$ ./sqlconnect.sh
SYSDATE
---------
22-AUG-09
$
Connect to a Database in Shell Programming and storing the output in UNIX variable
Use back ticks to do the same as
$ cat sqlconnect.sh
#!/bin/bash
ORACLE_VALUE=`sqlplus -s scott/tiger << EOFSQL
set head off
select ename from emp where empno=7839;
exit;
EOFSQL`
echo emp name : $ORACLE_VALUE
$ ./sqlconnect.sh
emp name : KING
$
To get access two columns data and storing result in Unix variables
$ cat sqlconnect.sh
#!/bin/bash
ORACLE_VALUE=`sqlplus -s scott/tiger << EOFSQL
set head off
select empno':'ename from emp where empno=7839;
exit;
EOFSQL`
echo emp no : `echo $ORACLE_VALUE cut -d ":" -f1`
echo emp name : `echo $ORACLE_VALUE cut -d ":" -f2`
$ ./sqlconnect.sh
emp no : 7839
emp name : KING
$
How to get data from a database table into UNIX file using shell script
Using a Shell Script to connect to sql database and execute db query from a unix shell script
$ cat sqlconnect.sh
#!/bin/bash
sqlplus -s scott/tiger << EOFSQL
spool emp.dat
select ename from emp where empno=7839;
spool off
exit;
EOFSQL
$ ./sqlconnect.sh
ENAME
----------
KING
$ ls -ltrtail -1
-rw-r--r-- 1 scripter scripterworld 245 Aug 22 10:55 emp.dat
$
Startup and shutdown Oracle Database with UNIX shell script
To startup oracle database from UNIX shell script
sqlplus -s "/ as sysdba" << EOF
startup;
exit;
EOF
To shutdown oracle database from unix shell script
sqlplus -s "/ as sysdba" << EOF
shutdown immediate;
exit;
EOF
UNIX shell script to run SQL files as
sqlplus -s scott/tiger << EOF
@emp.sql
@dept.sql
exit;
EOF
To pass unix variable from shell to Oracle database.
$ cat sqlconnect.sh
#!/bin/bash
echo -n "Enter empno number : "
read VALEMP
if [ -z "${VALEMP}" ]
then
echo "You shoul enter empno."
exit 1
fi
ORACLE_VALUE=`sqlplus -s scott/tiger << EOFSQL
set head off
select empno||':'||ename from emp where empno=${VALEMP};
exit;
EOFSQL`
echo emp no : `echo $ORACLE_VALUE | cut -d ":" -f1`
echo emp name : `echo $ORACLE_VALUE | cut -d ":" -f2`
$ ./sqlconnect.sh
Enter empno number : 7839
emp no : 7839
emp name : KING
$
To Call Shell script from SQL use HOST keyword
HOST /home/scripter/scripts/myshell.sh
Please give your valuable doubts or feedback via comments


To pass unix variable from shell to Oracle database.
$ cat sqlconnect.sh
#!/bin/bash
echo -n "Enter empno number : "
read VALEMP
if [ -z "${VALEMP}" ]
then
echo "You shoul enter empno."
exit 1
fi
ORACLE_VALUE=`sqlplus -s scott/tiger << EOFSQL
set head off
select empno||':'||ename from emp where empno=${VALEMP};
EOFSQL`
echo emp no : `echo $ORACLE_VALUE | cut -d ":" -f1`
echo emp name : `echo $ORACLE_VALUE | cut -d ":" -f2`
$ ./sqlconnect.sh
Enter empno number : 7839
emp no : 7839
emp name : KING
$
great information....really appreciate
Thanks for such neat explanation...
This really helpped me a lot!
I tried to run the full shutdown immediate for the database using one command (single shell line), but I couldn't make it working.
Regards
thanks alot really usefull man....
its very much help full
This is really helpful.
Thanks a lot but could you please answer how to connect to oracle remotely through shell script?
Hey,
Great work! Carry on this is awesome!
Appreciated:)
Hey its amazing dude!!Its really help me a lot after 3 days of workout.
Thanks a ton!!!
Very nicely explained .. Great work