How to Connect To MySQL or Oracle Database Using Shell Scripting

MRUTYUNJAYA GIRI
MRUTYUNJAYA GIRI used Ask the Experts™
on
I am Working on a Small Projects Using the Language shell scripting.
But I can't connect or link to database using shell scripting..
Please Help me Out.
Thank You....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
which shell script are you using,

you would need to use sqlplus for oracle and send commands and assign results,
for access to mysql you would use mysql ..
There are several references
http://www.oracle.com/technetwork/articles/linux/saternos-scripting-088882.html

For mysql
http://blog.mclaughlinsoftware.com/2015/05/17/bash-arrays-mysql/
Shell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
Commented:
Sample script for automation:
#!/bin/ksh
echo "SELECT TNAME FROM TAB;" |\
/u01/app/oracle/product/11.2.0/dbhome_2/bin/sqlplus Username/Password@Hostname:Port/ServiceName 2>&1|\
/bin/egrep -v "^SQL|^$"
# Commented line
# Write related full path of /u01/app/oracle/product/11.2.0/dbhome_2/bin/sqlplus

Open in new window

Inside the script
You can remove required strings which is not required:
echo "SQL
test
blank

last" |\
/bin/egrep -v "^SQL|^$"

Open in new window

Removing lines beginning with SQL => ^SQL
and removing blank lines => ^$ => Begins with => ^, ends with => $
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT

Commented:
Provided and tested the proposed command.
Resolving this query.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial