Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL: Query from within BASH script

Posted on 2011-09-09
2
614 Views
Last Modified: 2012-05-12
This MySQL query is successfully executed from a MySQL command line:
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` m WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )

Open in new window


However when I try to put it in a BASH script like this, I get an error:
echo "INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` m WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )" | mysql -u xyz -pabc123 -h m.example.com -D xyz_db

Open in new window



Group_Members: Command not found.
GroupID: Command not found.
MemberID: Command not found.
ID: Command not found.
Member: Command not found.
UserSuspended: Command not found.
PremPlusMember: Command not found.
PremWebMember: Command not found.
SMSMember: Command not found.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
0
Comment
Question by:hankknight
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 36512120
you don't run it from "bash" itself, but using mysql command line tool:
http://dev.mysql.com/doc/refman/5.5/en/mysql.html
mysql db_name < script.sql > output.tab

Open in new window

0
 
LVL 1

Assisted Solution

by:spock787
spock787 earned 200 total points
ID: 36513449
Assuming the command and SQL itself is correct, the backticks need to be excaped... try this...

echo "INSERT INTO \`Group_Members\` (\`GroupID\`,\`MemberID\`) SELECT '22', \`ID\` from \`Member\` m WHERE \`UserSuspended\` != '1' AND \`PremPlusMember\` != '1' AND \`PremWebMember\` = '1' AND \`SMSMember\` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )" | mysql -u xyz -pabc123 -h m.example.com -D xyz_db

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question