LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to access database through shell script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-access-database-through-shell-script-4175418726/)

Owais.Ahmad 07-26-2012 04:13 AM

How to access database through shell script
 
Hi,

I'm new to linux, I'm using Red Hat version 5. I have lamp installed in my system i want to retrieve database through shell script. My shell script's code is :

#!/bin/sh
clear

/opt/lampp/lampp start
/opt/lampp/bin/mysql -u root -p

This code opens the mysql client. But i want a script which displays the tables in the database. Can anyone help me with this????

Regards,

Owais

pan64 07-26-2012 04:19 AM

http://www.cyberciti.biz/faq/run-sql...-command-line/

Owais.Ahmad 07-26-2012 04:32 AM

well i want table in the database to be displayed on the command terminal.

lithos 07-26-2012 04:54 AM

Hi,

the first is what "pan64" has given you the link
the second I'm adding is
here

so you need to "use database" and "show tables" afterwards.

Here's the "one-line" code for showing databases:
Code:

#!/bin/sh
echo "show databases;" | /usr/bin/mysql -u root -p"password"



The output:
Database
information_schema
horde
mysql
postfix
test

and here listing the tables in "mysql" database:
Code:

echo "use mysql; show tables;" | /usr/bin/mysql -u root -p"password"


Output:
Tables_in_mysql
columns_priv
db
func
help_category
help_keyword
help_relation
help_topic
host
proc
procs_priv
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user


Owais.Ahmad 07-26-2012 05:12 AM

Thank you, it Worked :)


All times are GMT -5. The time now is 10:01 AM.