Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash variable under a mysql query

Tags:

linux

bash

mysql

I'm writing a bash script to clean wordpress based websites easily. I've a MySQL query to run with respect to a bash variable which I'm unable to figure out.

Since most of wordpress security plugins change database's table prefix so I want to use:

db_prefix=$(cat wp-config.php | grep "\$table_prefix" | cut -d \' -f 2)

to know of the exact table prefix which is in use & then use this MySQL query:

mysql -D${db_name} -u${db_user} -p${db_pass} << "EOF"
SELECT *
FROM  `wp_options`
WHERE  `option_name` LIKE  'template';
EOF

Now I want to use $db_prefix as variable on 3rd line of MySQL query by replacing wp_ with $db_prefix.

Is there any way to do so?

EDIT: I didn't know that it's that active, I really appreciate all of contributers, you'll see me contributing here too.

Bash script is live here now: https://github.com/saadismail/wp-clean

Thanks to all of you guys & girls...

like image 801
Saad Ismail Avatar asked Dec 31 '25 21:12

Saad Ismail


1 Answers

you must do as String

mysql -D${db_name} -u${db_user} -p${db_pass} -e "SELECT * FROM wp_${db_prefix}_options WHERE  option_name LIKE  'template';"

and it work.

or use backslash at the end of line

echo "SELECT * \
FROM  wp_${db_prefix}_options \
WHERE  option_name LIKE  'template " | mysql -D${db_name} -u${db_user} -p${db_pass}
like image 162
Bernd Buffen Avatar answered Jan 02 '26 10:01

Bernd Buffen