使用shell脚本生成数据库markdown文档
Posted 代码改变世界-Coding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用shell脚本生成数据库markdown文档相关的知识,希望对你有一定的参考价值。
学习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
以上是关于使用shell脚本生成数据库markdown文档的主要内容,如果未能解决你的问题,请参考以下文章