在Shell中执行Mysql语句

使用shell脚本做日志分析,为了将分析后的结果直接存储到mysql数据库中需要在shell脚本中执行mysql的语句。
方法一 将每一步需要执行的语句保存到 tmp.sql 中,最后在使用

  1.   mysql -u uname -p pwd < tmp.sql

方式执行

方法二 使用参数传递执行
mysql -u$user -p$pass -D $db -e “select afield from atable;”
: 对test.dat中的每一行拼装成sql做执行

  1. cat test.dat |awk '{print "insert into table(a, b) values(\047" $1 "\047, \047" $2 "\047)" }'|while read line
  2. do
  3.   mysql -u$user -p$pass -D $db -e $line
  4. done

其中 awk print命令中的 \047 代表单引号

Popularity: 7% [?]

Related

Comments

One Response to “在Shell中执行Mysql语句”

  1. Chaolen on December 21st, 2009 5:33 pm

    额..

    用不上linux..不懂!

    [Reply]