I'm busy writing a basic migration scripts for some WP sites, and i'm trying to automate creating mysql database and user credentials from reading the wp-config file
how can i read the following variables from the wp-config file so that i effectively end up with 3 variables within a bash script:
/** The name of the database for WordPress */
define('DB_NAME', 'somedbname');
/** MySQL database username */
define('DB_USER', 'someusername');
/** MySQL database password */
define('DB_PASSWORD', 'somerandompassword');
eg my output should effectively give me:
WPDBNAME=somedbname
WPDBUSER=somedbuser
WPDBPASS=somerandompassword
Try this:
WPDBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
If you want to use wp-config details to connect to mysql you can use;
mysql -u `cat wp-config.php | grep DB_USER | cut -d \' -f 4` -p`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4` -h `cat wp-config.php | grep DB_HOST | cut -d \' -f 4` `cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
you can use awk:
awk -F"[()']" '/^define/{printf "%s=\"%s\"\n", $3, $5;}' < foo.php
This will give you:
DB_NAME="somedbname"
DB_USER="someusername"
DB_PASSWORD="somerandompassword"
Note, that this solution will work with variables containing spaces.
find . -name "wp-config.php" -print0 | xargs -0 -r grep -e "DB_NAME" -e "DB_USER" -e "DB_PASSWORD"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With