Check if mysql database exists, perform action based on result

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

    I couldn't get the accepted answer work for me (the grep in the quotes didn't work), so here is my version:

    RESULT=`mysql -u $USER -p$PASSWORD --skip-column-names -e "SHOW DATABASES LIKE 'myDatabase'"`
    if [ "$RESULT" == "myDatabase" ]; then
        echo "Database exist"
    else
        echo "Database does not exist"
    fi
    

    I used the option --skip-column-names to remove the column names from the result.

提交回复
热议问题