LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-05-2011, 09:54 AM   #1
XardasCZ
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
Red face SQL commands inside bash script


Hi,

I'm trying create script to manage one mysql database, including new db and user creation.
But I'm not able get it working when I put SQL commands into function. So I create simple script for testing which is still not working :-(

Code:
#!/bin/bash -x

MAIN_DIR="/admin/mysql"
MYSQL_CNF="$MAIN_DIR/auth.cnf"
MYSQL_CMD="mysql --defaults-file=$MYSQL_CNF"

create_db_and_user(){
        $MYSQL_CMD <<-MYSQLEOF
                USE mysql;
                CREATE DATABASE testdb;
                CREATE USER 'testdb'@'localhost' identified by 'testdb';
                FLUSH PRIVILEGES;
                QUIT
        MYSQLEOF
        echo DONE
}

create_db_and_user
Output is following:

Code:
./test.sh
+ MAIN_DIR=/admin/mysql
+ MYSQL_CNF=/admin/mysql/auth.cnf
+ MYSQL_CMD='mysql --defaults-file=/admin/mysql/auth.cnf'
./test.sh: line 22: syntax error: unexpected end of file
Have you any idea why it's not working? Thanks for help
 
Old 05-05-2011, 10:08 AM   #2
XardasCZ
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Sorry, I'm stupid :-) Sign "-" in <<-MYSQLEOF means remove leading "TABs", I one line was spaces :-D

Quote:
Originally Posted by XardasCZ View Post
Hi,

I'm trying create script to manage one mysql database, including new db and user creation.
But I'm not able get it working when I put SQL commands into function. So I create simple script for testing which is still not working :-(

Code:
#!/bin/bash -x

MAIN_DIR="/admin/mysql"
MYSQL_CNF="$MAIN_DIR/auth.cnf"
MYSQL_CMD="mysql --defaults-file=$MYSQL_CNF"

create_db_and_user(){
        $MYSQL_CMD <<-MYSQLEOF
                USE mysql;
                CREATE DATABASE testdb;
                CREATE USER 'testdb'@'localhost' identified by 'testdb';
                FLUSH PRIVILEGES;
                QUIT
        MYSQLEOF
        echo DONE
}

create_db_and_user
Output is following:

Code:
./test.sh
+ MAIN_DIR=/admin/mysql
+ MYSQL_CNF=/admin/mysql/auth.cnf
+ MYSQL_CMD='mysql --defaults-file=/admin/mysql/auth.cnf'
./test.sh: line 22: syntax error: unexpected end of file
Have you any idea why it's not working? Thanks for help
 
  


Reply

Tags
bash scripting, mysql, sql


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing user and running commands inside of a script? cory94bailly Linux - Newbie 2 02-22-2011 12:57 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Running SQL commands from within bash chirayu11 Linux - Newbie 6 08-02-2010 06:38 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
where to place certiain commands inside shell script beeblequix Linux - Newbie 8 08-10-2006 11:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration