Guest User

Untitled

a guest
Feb 13th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Open mysql console with wordpress database credentials
  4.  
  5. if [ -z "$1" ]
  6. then
  7. echo " Usage: $0 path_to_wp_config" 1>&2
  8. exit 1
  9. fi
  10.  
  11. conffile=$1
  12.  
  13. if [ -d "$conffile" ]
  14. then
  15. conffile="${conffile%/}/wp-config.php"
  16. fi
  17.  
  18. if [ ! -f "$conffile" ]
  19. then
  20. echo "ERR: Couldn't find wordpress config file $conffile" 1>&2
  21. exit 1
  22. fi
  23.  
  24. grep_define() {
  25. grep -i define $conffile \
  26. | grep -v '//' \
  27. | grep -i $1 \
  28. | grep -oP ",[[:space:]]'\K[^']+"
  29. }
  30.  
  31. host=$( grep_define 'DB_HOST' )
  32. user=$( grep_define 'DB_USER' )
  33. pass=$( grep_define 'DB_PASSWORD' )
  34. dbna=$( grep_define 'DB_NAME' )
  35.  
  36. if [ -z "$host" ] || [ -z "$user" ] || [ -z "$pass" ] || [ -z "$dbna" ]
  37. then
  38. echo "ERR: Unable to extract wordpress database credentials from $conffile" 1>&2
  39. echo " Expected wordpress config file to define constants DB_HOST, DB_USER, DB_PASSWORD and DB_NAME" 1>&2
  40. exit 1
  41. fi
  42.  
  43. which mysql > /dev/null 2>&1 || {
  44. echo "ERR: Can't open database console. mysql not installed." 1>&2
  45. exit 1
  46. }
  47.  
  48. mysql -h"$host" -u"$user" -p"$pass" "$dbna"
Add Comment
Please, Sign In to add comment