在Shell中执行Mysql语句
使用shell脚本做日志分析,为了将分析后的结果直接存储到mysql数据库中需要在shell脚本中执行mysql的语句。
方法一 将每一步需要执行的语句保存到 tmp.sql 中,最后在使用
-
mysql -u uname -p pwd < tmp.sql
方式执行
方法二 使用参数传递执行
mysql -u$user -p$pass -D $db -e “select afield from atable;”
例: 对test.dat中的每一行拼装成sql做执行
-
cat test.dat |awk '{print "insert into table(a, b) values(\047" $1 "\047, \047" $2 "\047)" }'|while read line
-
do
-
mysql -u$user -p$pass -D $db -e $line
-
done
其中 awk print命令中的 \047 代表单引号
Popularity: 7% [?]
Related
Comments
One Response to “在Shell中执行Mysql语句”
额..
用不上linux..不懂!
囧
[Reply]