您的位置:首页 > 运维架构 > Shell

shell编程范例

2014-10-29 16:25 190 查看
common-config.sh
function initConfig(){
##配置库的数据库ip地址
configIpAddr=1
##端口
configPort=3306
##用户名
configUserName=
##密码
configPassword=
##数据库
configDatabaseName=
##
configSyncStatus=0
##
configStatus=1
##
mysqlDateFormat="%Y-%m-%d %H:%i:%S"
##
linuxDateFormat="%Y-%m-%d %H:%M:%S"
##
linuxThreadCount=10
}
pos-config.sh
#!/bin/bash

source ./common-config.sh

##初始化变量
function initVar(){
  initConfig
  ##mysql的ip地址
  ipAddr=
  ##端口
  port=3306
  ##用户名
  userName=retail_pos
  ##密码
  password=retail_pos
  ##数据库
  databaseName=retail_pos
  ##业务数据的分页大小
  dataPageSize=1000
  ##任务的分页大小
  configPageSize=10
  ##组名
  groupName=7006
  ##触发器名
  triggerName=pos
  ##查询sql语句
  configSelectSql="select id,schema_name,table_name,sync_col_name,sync_begin_time,sync_freq,(case when (unix_timestamp(now())-unix_timestamp(sync_begin_time)>sync_freq+(sync_freq/2)) then now() else date_add(sync_begin_time,interval sync_freq second) end) as sync_end_time from task_status where group_name = '$groupName' and trigger_name = '$triggerName' and config_status = $configStatus and sync_status in ($configSyncStatus)"
}
pos_main.sh
##使用bash解析器
#!/bin/bash

##引用资源
source ./pos-config.sh

##主方法
function main(){
  initVar
  loadTask
}

##加载配置文件的任务
function loadTask(){
  local taskCount=0
  mysql -h$configIpAddr -P$configPort -D$configDatabaseName -u$configUserName -p$configPassword -s -N -e "$configSelectSql" |
  while read id schema_name table_name sync_col_name sync_begin_time sync_end_time; do 
    sh /usr/local/wonhigh/test/pos_task.sh $schema_name $table_name $sync_col_name $sync_begin_time $sync_end_time
    if [ $linuxThreadCount -gt $taskCount ]; then
      taskCount=`expr $taskCount + 1`
    else
      local threadNum=`ps -ef | grep "pos_task.sh" | grep -v grep | wc -l`
      for((;$linuxThreadCount <= $threadNum;)) do
        local threadNum=`ps -ef | grep "pos_task.sh" | grep -v grep | wc -l`
        if [ $linuxThreadCount -gt $threadNum ]; then
          taskCount=`expr $taskCount - 1`
          break
        fi
      done;
    fi
  done;
}

main
<pre name="code" class="plain">pos_task.sh
<pre name="code" class="plain">source /usr/local/wonhigh/test/pos-config.sh

initVar

function loadData(){
  local sync_begin_time=`date -d "$4" +"%Y-%m-%d %H:%M:%S"`
  local sync_end_time=`date -d "$5" +"%Y-%m-%d %H:%M:%S"`
  local dataCountSql="select count(*) from $1.$2 t where $3 >= str_to_date('$sync_begin_time','%Y-%m-%d %H:%i:%S') and $3 < str_to_date('$sync_end_time','%Y-%m-%d %H:%i:%S')"
  local dataTotalCount=`mysql -h$ipAddr -P$port -D$databaseName -u$userName -p$password -N -e "$dataCountSql"`
  local dataSqlStr="select t.* from $1.$2 t where $3 >= str_to_date('$sync_begin_time','%Y-%m-%d %H:%i:%S') and $3 < str_to_date('$sync_end_time','%Y-%m-%d %H:%i:%S') "
  local forCount=`expr $dataTotalCount / $dataPageSize`
  for((i=0; i<=$forCount; i++)); do
    dataSqlStr1="$dataSqlStr limit $i,$dataPageSize"
echo $dataSqlStr1
    mysql -h$ipAddr -P$port -D$databaseName -u$userName -p$password -s -N -e "$dataSqlStr1" > /usr/local/wonhigh/test/$1.$2.log
  done
}

loadData "$@"

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux shell function mysql hive