学习shell脚本编程的一次实践,通过shell脚本生成数据库的markdown文档,代码如下:
HOST=xxxxxx PORT=xxxx USER="xxxxx" PASSWORD="xxxxxx" DATABASE_PREFIX="xxxxxx" QUERY_DATABASE="select distinct TABLE_SCHEMA from information_schema.TABLES where TABLE_TYPE='BASE TABLE'" databaseList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -e "${QUERY_DATABASE}") for database in ${databaseList} do if [[ ${database} == *${DATABASE_PREFIX}* ]] then echo "Start ${database}" echo "### ${database}" >> test.md queryTable="select distinct TABLE_NAME from information_schema.COLUMNS where TABLE_SCHEMA='${database}'" tableList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -N -e "${queryTable}") for table in ${tableList} do if [ $(grep -c "#### ${table%_*}_0" test.md) -ne '0' ] then continue fi echo "Start print table:${table}" echo "#### ${table}" >> test.md echo "名称 | 类型 | 是否可为空 | Key | 说明" >> test.md echo "--|--|--|--|--" >> test.md queryCloumns="select COLUMN_NAME, replace(COLUMN_TYPE,' ',''),IS_NULLABLE,CONCAT(COLUMN_KEY,'+++++++++'),CONCAT(replace(COLUMN_COMMENT,' ',''),'+++++++++') from information_schema.COLUMNS where TABLE_NAME='${table}' and TABLE_SCHEMA='${database}' order by ORDINAL_POSITION" columnList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -N -e "${queryCloumns}") count=0 row="" for column in ${columnList} do row="${row} ${column} | " let count++ if [[ ${count} == 5 ]] then echo ${row} >> test.md count=0 row="" fi done done fi done