Check if mysql database exists, perform action based on result

前端 未结 20 2436
傲寒
傲寒 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:32

    mysqlshow is a good tool for this, here is test to check the presence of the database database_name

    if mysqlshow -p${MYSQL_ROOT} 2>/dev/null| grep -q "database_name"
    then
        echo "Database exist."
    else
        echo "Database does not exist."
    fi
    

    Or a simple oneliner

    echo "Database "`mysqlshow -p${MYSQL_ROOT} 2>/dev/null| grep -q "database_name"  || echo "does not "`"exist."
    

提交回复
热议问题