Check if mysql database exists, perform action based on result

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

    Also you can ask to use the database and then handle the exit code.

    $ if mysql -uroot -pxxx -e "USE mysql"; then echo "exists"; fi
    exists
    
    $ if mysql -uroot -pxxx -e "USE doesnotexist"; then echo "exists"; fi
    ERROR 1049 (42000) at line 1: Unknown database 'doesnotexist'
    

    Or inspect $? after the call.

提交回复
热议问题