Reputation: 27
Im trying to make a oneliner in Linux that will collect database host, database name, username and password from wp-config.php
and combine that into a line where i can import a database with a .sh
file.
The block of code I need to collect is:
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');
The line I'm trying to create is:
mysql -h localhost -u username_here --password="password_here" database_name_here < db-backup.sql
I'm new to this so what i have tried to do is:
grep DB_HOST DB_USER wp-config.php | cut -d "'" -f4 | awk '{print "mysql -h "$0;}'
This start is working, but when i want to add more info to the string im just getting the last part.
How do I write this to get a full row with all information?
Upvotes: 0
Views: 83
Reputation: 310
not a one-liner, but works for your simple example
DB_PASSWORD=$(grep DB_PASSWORD wp-config.php | cut -d "'" -f4)
DB_USER=$(grep DB_USER wp-config.php | cut -d "'" -f4)
DB_NAME=$(grep DB_NAME wp-config.php | cut -d "'" -f4)
DB_HOST=$(grep DB_HOST wp-config.php | cut -d "'" -f4)
echo "mysql -h $DB_HOST -u $DB_USER --password=\"$DB_PASSWORD\" $DB_NAME"
Few notes: you do not need awk, you used it to just concatenate strings, so I removed it. And the detection of the relevant lines in the input file is not perfect, it may be confused by many things, for example if there is a comment in the input file
/** we are using blabla as DB_NAME because <reasons> **/
It would detect that line as matching for DB_NAME. Also, it is important to have exactly one matching line.
Upvotes: 0
Reputation: 123
MYSQLPWD=$(cat wp-config.php |grep DB_PASSWORD | cut -d \' -f 4)
MYSQLUSR=$(cat wp-config.php |grep DB_USER | cut -d \' -f 4)
MYSQLDBNAME=$(cat wp-config.php |grep DB_NAME | cut -d \' -f 4)
HT="localhost"
echo $MYSQLPWD
echo $MYSQLUSR
echo $MYSQLDBNAME
echo $HT
mysql -h$HT -u$MYSQLUSR -p$MYSQLPWD $MYSQLDBNAME < $MYSQLDBNAME
Upvotes: 1