检测数据库中指定表是否存在指定字段脚本

check_mysql_field_exist.sh

    #!/bin/bash
    
    #---------------------------------------
    # 检测数据库是否存在指定字段 变量1:表名 变量2:字段名
    # 注:数据库名同文件夹名
    # author: SenSen 1050575278
    # date: 2021/10/10
    # 注意:所有脚本需测试后方可使用!!!
    #---------------------------------------
    
    if [ "$1" ] && [ "$2" ]; then
    
      #获取当前时间
      curtime=`date +"%Y/%m/%d %H:%M:%S"`
    
      #所在目录
      dir='/web/oa/'
    
      #连接数据库
      db_con="/usr/local/mysql/bin/mysql -uxxx -pxxx"
    
      #自动获取系统目录文件 仅可包含oa系统文件
      sysDir=`ls /web/oa`
    
      for db in ${sysDir}
          do
              ${db_con} -e "select count(*) from information_schema.columns where table_schema = '${db}' and table_name = '${1}' and column_name = '${2}';" >> /web/shell/check_mysql_field_exist.txt
              echo ${db} " done" >> /web/shell/check_mysql_field_exist.txt
          done
    else
        echo "Error: param is empty"
    fi

Tags: 脚本

添加新评论