1

Hello I'm trying to run my ssh bash script from root (which has access to all hundreds of databases on my server). I'm trying to get the database name of my wordpress websites without having to login and open the file and copy and paste the db name. I'm doing this so I can then continue on to do other things in the particular database using the scripts.

So the line item for the database in file wp-config.php looks like this always: name define( 'DB_NAME', 'xyz' );

I'm simply trying to find that line item and then get/extract and use that db name (ie. xyz) later in the script (ie. $db). That's it.

I'm assuming using grep/sed/regex or combo or something would do it but I can't find out how. Let me know if anyone knows how to do this?

1
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). Commented Aug 19, 2020 at 19:59

2 Answers 2

2

This is a suitable task for sed. Selecting the matching define line using a regexp and extract the value using a capture group:

db=$(sed -n "s/define( *'DB_NAME', *'\([^']*\)'.*/\1/p" wp-config.php)
echo $db
Sign up to request clarification or add additional context in comments.

2 Comments

Ok great thanks for the response! What variable would i use to pass the value on? ie if I used $db or something. How would I call that back up on the next line of the script?
You need shell command substitution to store the result. I have added the coded to the answer
0

This code is worked for me i have extrected the database details from wp-config.php and past it to myfile.php

//Path to your wp-config.php file
$wp_config_path = 'wp-config.php';
// Include the wp-config.php file
if (file_exists($wp_config_path)) {
    include($wp_config_path);
    // Retrieve the database credentials
    $db_name = defined('DB_NAME') ? DB_NAME : '';
    $db_user = defined('DB_USER') ? DB_USER : '';
    $db_password = defined('DB_PASSWORD') ? DB_PASSWORD : '';
    $db_host = defined('DB_HOST') ? DB_HOST : '';
} else {
    echo "wp-config.php not found or invalid path.";
}

Note:Make sure your path is correct, or your file and the wp-config is in the same folder..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.