需求
不同服务器上的mongo数据库可能不一样,而且可能数据库表会增加或者删减。因此写成固定的列表无法满足需求
方案
通过与shell交互操作实现mongo命令查看数据库表项
代码
#!/bin/bash
set -x
# collections=mongo $dbName --eval "db.getCollectionNames()"
# echo $collections
ret=`mongo << EOF
show dbs
EOF`
# mongo version format xx.xx.xx
dbRet=`echo $ret|grep -Eo "version: .+"|sed 's/version:[[:space:]][0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}[[:space:]]//g'|sed 's/[[:space:]]bye//g'`
# get db name
n=0
dbList=()
for i in ${dbRet[@]}
do
if [[ `echo $i |grep GB` ]]
then
continue
fi
dbList[n]=$i
let n++
done
echo -----dbList:${dbList[*]}-----
# 后面遍历或者其它处理即可