shell map使用


 

 

# 定義初始化map
declare -A map=(["100"]="1" ["200"]="2")

# 輸出所有key
echo ${map[@]}

# 輸出key對應的值
echo ${map["100"]}

# 遍歷map
for key in ${!map[@]}
do
    echo ${map[${key}]}
done

 

#!/bin/bash
#********************************************************************#
##author:郭昊
##create time:2019-04-03 14:09:17
#********************************************************************#
source /webser/odps/shell/odps.sh

### 定義變量
confSql="select concat_ws('|',check_id,check_desc,type,sql_txt) as flag from check_erroer_data_config where is_deleted = 0"
typeMap=(["a"]="1" ["b"]="2" ["c"]="3" ["d"]="4" ["e"]="5")

### 主函數
main(){
    # 清空結果表歷史數據
    db_query_retant "bigdata_db" "truncate table check_erroer_data_result"

    # 遍歷配置表里的記錄
    db_query_retant "bigdata_db" "${confSql}"|sed '1d'|while read line
    do
        check_id=$(echo ${line}|awk -F'|' '{print $1}')
        check_desc=$(echo ${line}|awk -F'|' '{print $2}')
        type=$(echo ${line}|awk -F'|' '{print $3}')
        sql_txt=$(echo ${line}|awk -F'|' '{print $4}')

        # 處理單引號
        sql=$(echo ${sql_txt}|sed "s/'/''/g")

        # 處理執行語句
        sql_excute=$(echo ${sql_txt}|sed "s/select/select ${check_id} as check_id,'${check_desc}' as check_desc,'${type}' as type,'${sql}' as sql_txt,DATABASE() as db_name,/i")
        echo "${sql}"
        echo "${sql_excute}"

        # 獲取key對應的值
        typeTenants=${typeMap[${type}]}

        # 統計異常數據量
        if [ "${typeTenants}" != "" ]
        then
            if [ "${type}" == "gcxt" ]
            then
                upload_1 "${typeTenants}" "${sql_excute}" "check_id,check_desc,type,sql_txt,db_name,error_cnt" "check_erroer_data_result" "bigdata_db" 0
            else
                upload_2 "${typeTenants}" "${sql_excute}" "check_id,check_desc,type,sql_txt,db_name,error_cnt" "check_erroer_data_result" "bigdata_db" 0
            fi
        else
            echo "[ERROR] Type: [ ${type} ] not match,Please check again"
        fi
    done

    # 刪除正常的記錄
    db_query_retant "bigdata_db" "delete from check_erroer_data_result where error_cnt = 0"
}

### 運行主函數
main

 

-- 表結構設計
use bigdata_db;
-- 配置表
drop table check_erroer_data_config;
create table check_erroer_data_config(
     check_id     tinyint       primary key auto_increment comment '檢查項序號'
    ,check_desc   varchar(500)  not null                   comment '檢查項描述'
    ,type         varchar(100)  not null                   comment '檢查項類型'
    ,sql_txt      varchar(1000) not null                   comment '檢查SQL'
    ,is_deleted   tinyint       default 0                  comment '是否已刪除'
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='業務異常數據巡檢配置表'
;

-- 結果表
drop table check_erroer_data_result;
create table check_erroer_data_result(
     check_id     tinyint       not null                           comment '檢查項序號'
    ,check_desc   varchar(500)  not null                           comment '檢查項描述'
    ,type         varchar(100)  not null                           comment '檢查項類型'
    ,db_name      varchar(50)   not null                           comment '檢查數據庫名'
    ,sql_txt      varchar(1000) not null                           comment '檢查SQL'
    ,error_cnt    int(11)       default 0                          comment '異常記錄數'
    ,update_time  timestamp     DEFAULT CURRENT_TIMESTAMP not null comment '更新時間'
    ,primary key(db_name,check_id)
    ,KEY `ix_error_cnt` (`error_cnt`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='業務異常數據巡檢結果表'
;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM