0
votes

I teach students how to fix wordpress sites and I would like to write a script that looks at what information they have in their wp-config.php and add a single letter or number to the database name.

For example the line is such

define('DB_NAME', 'cpanelUser_NameofDB');

I would like to add a number or a line to the end of NameofDB

I can use this to isolate the cpanelUser_NameofDB

grep -i 'DB_NAME' wp-config.php | cut -d"'" -f4

but I'm not sure how to add information, nor if this is the correct script I should run to get there. I would also like it to not matter what the name of the database is since it will be ran on multiple sites. I'm sure I could use regex but I'm not too versed in that and would not know where to start. Help please!

1
Did you think about the possibiltity, that the define statement might span several lines, including arbitrary comments? - user1934428

1 Answers

0
votes

You can use sed for this purpose to fix this line:

#!/bin/bash

id=34

sed -i "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" wp-config.php

flag -i means that we do change in file directly.

Alternatively you can make template file and generate new file while sed works on stdin and stdout

#!/bin/bash

id=34

cat wp-config.php-template| sed "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" > wp-config.php.$id