Chinaunix

标题: 如何把sql写到SHELL里 然后让他每天动态执行 然后再把sql的结果用shell带出来 [打印本页]

作者: ssxssx    时间: 2013-10-31 11:59
标题: 如何把sql写到SHELL里 然后让他每天动态执行 然后再把sql的结果用shell带出来
比如
select substr(create_time, 1, , count(*)
  from t_sys_writer t
where    substr(create_time, 1, between'20131028' and '20131028'
   and status = 1
group by substr(create_time, 1,
order by substr(create_time, 1, ;
我想把这句sql写到shell里  然后between 和and之间的时间  我由shell脚本每天执行带进来  就是每次执行脚本都传进来昨天的时间    而且我要把由sql计算的结果在从SHELL里带出来  请教该怎么做  因为我要写成脚本的形式  让它动态执行

作者: 关阴月飞    时间: 2013-10-31 12:26
如果数据库是mysql:

  1. #!/bin/bash
  2. DATE=$1
  3. mysql -e "select substr(create_time, 1, , count(*)
  4.   from t_sys_writer t
  5. where    substr(create_time, 1,  between'${DATE}' and '20131028'
  6.    and status = 1
  7. group by substr(create_time, 1,
  8. order by substr(create_time, 1, ;"
复制代码

作者: ssxssx    时间: 2013-10-31 12:55
回复 2# 关阴月飞
数据库是ORALCE啊  你能写的在简单点吗  看不懂啊 到底怎么把结果传出去呢


   
作者: Shell_HAT    时间: 2013-10-31 13:23
回复 3# ssxssx


在sqlplus里面用spool把结果写入文件
用shell读这个文件
http://bbs.chinaunix.net/thread-15827-1-1.html
作者: dandy1992    时间: 2013-10-31 14:23

##init
function init(){
    source /etc/profile
    ip=$1
    yesterday=`date +"%Y%m%d" -d '-1 day'`
    now=`date +"%Y%m%d"`
}

function executeSql()
{

  sql="$1"
  if [ "$sql"x = ""x ];then
    cat | mysql -h10.10.10.${ip}  -uroot test -N --local-infile
  else
    echo "$sql" | mysql -h10.10.10.${ip}  -uroot test zhenai_crm -N --local-infile
  fi
}

function main(){
    init ""
    echo "select * from tablename where createtime>='${yesterday}' and createtime<'${now}'" | executeSql >>res.tmp
}

main ""

##类似你的需求,把几个函数封装了一下,你自己改下tablename ip 还有test数据库吧




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2