話不多說,直接上code
python 腳本:test_mysql.py
#!/user/bin/env python
#coding=utf-8
import subprocess
def system_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
result = process.stdout.readlines()
return result
if __name__ == "__main__":
result = system_command('/root/test_mysql.sh')
print result
shell腳本:test_mysql.sh
#!/bin/bash
exe_secelt(){
HOST_NAME="192.168.165.101";
PORT="3306";
USER_NAME="root";
PASS_WORD="123456";
DBNAME="pear1.9.0-data";
TABLE_NAME="host";
HOST=$1;
select_sql="select * from ${TABLE_NAME} where name='${HOST}'";
mysql -h${HOST_NAME} -P${PORT} -u${USER_NAME} -p${PASS_WORD} ${DBNAME} -e "${select_sql}"
}
HOST=$1;
exe_secelt "$HOST"
運行python 腳本
python test_mysql.py
注:在編寫shell的工程中發現一個很有趣的mysql問題
在此腳本中,我select了名稱叫order的表,報錯:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL serve
r version for the right syntax to use near 'order' at line 1
原因竟是因爲我order表中存在名爲order的字段。