Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Open mysql console with wordpress database credentials
- if [ -z "$1" ]
- then
- echo " Usage: $0 path_to_wp_config" 1>&2
- exit 1
- fi
- conffile=$1
- if [ -d "$conffile" ]
- then
- conffile="${conffile%/}/wp-config.php"
- fi
- if [ ! -f "$conffile" ]
- then
- echo "ERR: Couldn't find wordpress config file $conffile" 1>&2
- exit 1
- fi
- grep_define() {
- grep -i define $conffile \
- | grep -v '//' \
- | grep -i $1 \
- | grep -oP ",[[:space:]]'\K[^']+"
- }
- host=$( grep_define 'DB_HOST' )
- user=$( grep_define 'DB_USER' )
- pass=$( grep_define 'DB_PASSWORD' )
- dbna=$( grep_define 'DB_NAME' )
- if [ -z "$host" ] || [ -z "$user" ] || [ -z "$pass" ] || [ -z "$dbna" ]
- then
- echo "ERR: Unable to extract wordpress database credentials from $conffile" 1>&2
- echo " Expected wordpress config file to define constants DB_HOST, DB_USER, DB_PASSWORD and DB_NAME" 1>&2
- exit 1
- fi
- which mysql > /dev/null 2>&1 || {
- echo "ERR: Can't open database console. mysql not installed." 1>&2
- exit 1
- }
- mysql -h"$host" -u"$user" -p"$pass" "$dbna"
Add Comment
Please, Sign In to add comment