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 "$@"
相关文章推荐
- shell编程范例之字符串操作[转]
- shell编程范例之字符串操作
- [授权发表]Shell编程范例之索引篇
- shell编程范例之数值运算
- [授权发表]Shell编程范例之开篇
- [授权发表]Shell编程范例之数值运算
- shell编程范例之进程操作
- [授权发表]Shell编程范例之布尔运算
- [授权发表]Shell编程范例之字符串操作
- [授权发表]Shell编程范例之文件操作
- shell编程范例之字符串操作[转]
- [授权发表]Shell编程范例之文件系统操作
- [授权发表]Shell编程范例之进程操作
- [授权发表]Shell编程范例之网络操作
- [授权发表]Shell编程范例之总结篇
- C++中的vector使用范例
- Shell编程总结及感悟
- shell编程学习之sed
- shell编程总结及感悟
- jQuery Validation范例