Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute mysql query in shell script

Suppose i have 2 databases nm and nm_history. I need to get records from both databases.if i execute query in mysql, its working fine. But if i execute that query in bin/bash it returns following error

**

mysql: option '-e' requires an argument
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: a_party: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: prov_channel: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: created: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: created: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: a_party: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: msisdn: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: action_type: command not found
./get_rec.sh: line 4: SELECT nm.., nm.., nm.. AS Created, nm_history.. AS terminate FROM nm. INNER JOIN nm_history.  ON nm.. = nm_history..
WHERE nm_history..=2: command not found

**

here is the script:

mysql -uUser -pPassword -hHostDB -e

The query:

SELECT S.`a_party`, S.`prov_channel`, S.`created` AS Created,M.`created` AS terminate FROM nm.`subscriber_1` S INNER JOIN nm_history.`svc_mgmt_06` M ON S.`a_party` = M.`msisdn` 
WHERE M.`action_type`=2;**
like image 721
user3189059 Avatar asked Jul 03 '14 05:07

user3189059


2 Answers

The -e argument needs an inline argument, as followed:

mysql -uUSER -pPASS -hHOST -e "SELECT * FROM db.table;"
like image 179
Nicop Avatar answered Oct 30 '22 23:10

Nicop


you should write query string in shell script without backticks (`):

mysql -uUser -pPassword -hHostDB -e"SELECT S.a_party, S.prov_channel, S.created AS Created,M.created AS terminate 
FROM nm.subscriber_1 S 
INNER JOIN nm_history.svc_mgmt_06 M ON S.a_party = M.msisdn
WHERE M.action_type=2"
like image 43
kiba Avatar answered Oct 30 '22 23:10

kiba