Guest User

Untitled

a guest
Aug 25th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. bash script - check if mysql database exists peform action based on result
  2. #!/bin/bash
  3. ## --user=XXXXXX --password=XXXXXX *may* not be necessary if run as root or you have unsecured DBs but
  4. ## using them makes this script a lot more portable. Thanks @billkarwin
  5. RESULT=`mysqlshow --user=XXXXXX --password=XXXXXX myDatabase| grep -v Wildcard | grep -o myDatabase`
  6. if [ "$RESULT" == "myDatabase" ]; then
  7. echo YES
  8. fi
  9.  
  10. [root@host ~]# mysqlshow myDatabase
  11. Wildcard: myDatabase
  12. +------------------+
  13. | Databases |
  14. +------------------+
  15. | myDatabase |
  16. +------------------+
  17.  
  18. [root@host ~]# mysqlshow myDatabase
  19. Wildcard: myDatabase
  20. +-----------+
  21. | Databases |
  22. +-----------+
  23. +-----------+
  24.  
  25. if [ -d /var/lib/mysql/databasename ] ; then ...
  26.  
  27. for db in $(mysql -u -p -N <<<"show databases like '%something%'")
  28. do
  29. case $db in
  30. "something")
  31. // do something
  32. ;;
  33. "something else")
  34. // do something else
  35. ;;
  36. esac
  37. done
  38.  
  39. RESULT=`mysql -u $USER -p$PASSWORD --skip-column-names -e "SHOW DATABASES LIKE 'myDatabase'"`
  40. if [ "$RESULT" == "myDatabase" ]; then
  41. echo "Database exist"
  42. else
  43. echo "Database does not exist"
  44. fi
Add Comment
Please, Sign In to add comment