Check if mysql database exists, perform action based on result

前端 未结 20 2058
傲寒
傲寒 2021-02-01 16:02

Is it possible from within a bash script to check if a mysql database exists. Depending on the result then perform another action or terminate the script?

20条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 16:18

    Here is an alternate version:

     RESULT=`mysql -u$USER -p$PASSWORD -e "SHOW DATABASES" | grep $DATABASE`
     if [ "$RESULT" == "$DATABASE" ]; then
        echo "Database exist"
     else
        echo "Database does not exist"
     fi
    

    IF there is a DB named abcd and we use -Fo after grep then for the search result of DB a/ab/abc the script will show the result Database exist.

提交回复
热议问题