編寫Shell腳本執行SQL語句,並將結果發送到釘釘羣

首先編寫mysql腳本,然後在crontab中編寫定時任務觸發這個腳本,向釘釘羣裏發消息

#!/bin/bash
sevenDate=$(date -d -7day '+%Y-%m-%d %H:%M:%S')
libraryHost="你的mysql域名"
libraryDB="mysql -u用戶名 -p密碼 -h${libraryHost} --default-character-set=utf8 -A -N"

sql1="SELECT t0.lesson_id,'%' separ1,t3.name,'%' separ2,t1.ids1,'%' separ3,t2.ids2,'#' endline FROM
(SELECT DISTINCT(lesson_id) FROM library.course_user_task_record WHERE finish_time>'$sevenDate' AND finish_time<NOW() AND modify_status=2) t0
LEFT JOIN (SELECT lesson_id,GROUP_CONCAT(id) AS ids1 FROM library.course_user_task_record WHERE finish_time>'$sevenDate' AND finish_time<NOW() AND modify_status=2 AND finish_time<=DATE_SUB(NOW(),INTERVAL 24 HOUR) GROUP BY lesson_id )t1 ON t0.lesson_id=t1.lesson_id
LEFT JOIN (SELECT lesson_id,GROUP_CONCAT(id) AS ids2 FROM library.course_user_task_record WHERE finish_time>'$sevenDate' AND finish_time<NOW() AND modify_status=2 AND finish_time<=DATE_SUB(NOW(),INTERVAL 18 HOUR) AND finish_time>DATE_SUB(NOW(),INTERVAL 24 HOUR) GROUP BY lesson_id ) t2 ON t0.lesson_id=t2.lesson_id
LEFT JOIN library.course_lesson t3 ON t0.lesson_id=t3.id WHERE (t1.ids1 IS NOT NULL) OR (t2.ids2 IS NOT NULL) OR (t1.ids1 IS NOT NULL AND t2.ids2 IS NOT NULL);"

sql1Result="$($libraryDB -e "$sql1")"
content=""
if [ ${#sql1Result[@]} = "0" ]
then
    content="本時段沒有要提醒審覈的作業"
fi

#sql1Result=${sql1Result//[ ,  ,
#]/}

OLD_IFS="$IFS"
IFS="#"
resultArray=($sql1Result)
for elem in ${resultArray[@]}
do
    IFS="%"
    rowData=($elem)
    IFS="$OLD_IFS"
    desc=""
    for idx in " ${!rowData[@]} "
    do
        if [ $idx -eq 1 ];then
            desc="課程:【${rowData[1]}】"
        elif [ $idx -eq  2 ];then
             total=0
             str=${rowData[2]};
             if [ $str == "NULL" ]; then
                    total=0
             else
                    OLD_IFS="$IFS"
                    IFS=","
                    arr=($str)
                    total=${#arr[@]}
                    IFS="$OLD_IFS"
             fi
             desc="$desc,超時未批改作業:【$total :${rowData[2]}】"
        elif [ $idx -eq 3 ];then
             total=0
             str=${rowData[3]};
             if [ $str == "NULL" ]; then
                    total=0
             else
                    OLD_IFS="$IFS"
                    IFS=","
                    arr=($str)
                    total=${#arr[@]}
                    IFS="$OLD_IFS"
             fi
             desc="$desc,即將超時未批改作業:【$total :${rowData[3]}】"
        fi
    done

    emptyString=""
    if [ "$content" == "$emptyString" ]; then
        content="$content$desc"
    else
        content="$content \n$desc"
    fi
done

echo $content
#curl "https://oapi.dingtalk.com/robot/send?access_token=你的accesstoken" -H 'Content-Type:application/json' -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$content\"}}"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章