Bash - Check if a mysql database existed
I use mysqlshow command to check if a mysql database existed, check_db_existed.sh:
#! /bin/bash
# $1: db username
# $2: db password
# $3: db name
#RESULT=`mysqlshow -u $1 -p"$2" $3 | grep -v Wildcard | grep -o $3`
RESULT=`mysql -u $1 -p"$2" --skip-column-names -e "SHOW DATABASES LIKE '$3'"`
if echo "$RESULT" | egrep -q "$3" ; then
echo "$3 existed"
else
echo "$3 not existed"
fi
And call the script to check if 'mydb' existed, (root is db username and password) :
# sh check_db_existed.sh root root mydb
#! /bin/bash
# $1: db username
# $2: db password
# $3: db name
#RESULT=`mysqlshow -u $1 -p"$2" $3 | grep -v Wildcard | grep -o $3`
RESULT=`mysql -u $1 -p"$2" --skip-column-names -e "SHOW DATABASES LIKE '$3'"`
if echo "$RESULT" | egrep -q "$3" ; then
echo "$3 existed"
else
echo "$3 not existed"
fi
And call the script to check if 'mydb' existed, (root is db username and password) :
# sh check_db_existed.sh root root mydb
Comments
Post a Comment