I can't figure out why my bash script is printing the results of a mysql query with the name of a table column at the beginning of output...
here's a part of the script:
Code
function wpplug {
wp_sql "SELECT option_value FROM %table_prefix%options WHERE option_name='active_plugins';"
}
function wp_sql {
args=$@
local query=$(IFS=" "; echo "${args[*]}")
if [[ ! -z $1 ]]; then
local dbcredstr=$(wpdb_creds)
IFS=" " read -a dbcreds <<< "$dbcredstr"
if [ "${#dbcreds[@]}" -eq 5 ]; then
# Replace %table_prefix% with the wordpress db table prefix stored in ${dbcreds[4]}
sql_query=$(echo $query | sed "s/%table_prefix%/${dbcreds[4]}/g")
results=$(mysql -u "${dbcreds[3]}" -p"${dbcreds[2]}" -h "${dbcreds[0]}" -D "${dbcreds[1]}" -e "$sql_query" 2>&1)
echo $results
fi
fi
}
function wpdb_creds {
local domain_root=$(wp_domain_root)
if [[ ${#domain_root} -ge 5 ]]; then
read -r dbhost dbname dbpass dbuser dbprefix <<< $(cat $domain_root"wp-config.php" | egrep "^[^/].*[\"']DB_(NAME|USER|PASSWORD|HOST[^_])|table_prefix" | sort -d | sed "s/.*[\"']\(.*\)[\"'].*;.*/\1/" )
# Print db info in wp-config.php
if [[ ! -z $1 && $1 == '-p' ]]; then
echo -e $cBlue"\nWP Database Info\n\tHostname: "$cDone$dbhost"\n\t"$cBlue"Database: "$cDone$dbname"\n\t"$cBlue"Password: "$cDone$dbpass"\n\t"$cBlue"Username: "$cDone$dbuser"\n\t"$cBlue"Prefix: "$cDone$dbprefix"\n";
# Return database credentials for use outside of local function scope.
else
local dbcreds=($dbhost $dbname $dbpass $dbuser $dbprefix)
echo "${dbcreds[@]}"
fi; else echo -e $cRed'Could not find a wp-config.php file...'$cDone; fi
}
function wp_domain_root {
found=false;local root='';cwd=${PWD}
IFS="/" read -a dirs <<< "$cwd"
for((i="${#dirs[@]}"; i>0; i--)); do
tmp_dir=""
for((ii=0;ii < $i; ii++)); do
tmp_dir+="${dirs[ $ii ]}/"
done
if [ -f $tmp_dir"/wp-config.php" ]; then root=$tmp_dir; break; fi
done
echo $root
}
when i run this script by injecting into my ssh session and type wpplug, the query executes successfully and prints. However, before it prints out the data im grabbing, it also prints 'option_value' before... output:
Code
option_value a:22:{i:0;s:23:"anti-spam/anti-spam.php";i:1;s:19:"captcha/captcha.php";i:2;s:36:"contact-form-plugin/contact_form.php";i:3;s:48:"default-facebook-thumbnail/mult_fb_thumbnail.php";i:4;s:39:"easy-facebook-share-thumbnails/esft.php";i:5;s:37:"email-newsletter/email-newsletter.php";i:6;s:15:"email/email.php";i:7;s:53:"facebook-like-box-widget/facebook-like-box-widget.php";i:8;s:55:"facebook-share-statistics/facebook-share-statistics.php";i:9;s:43:"flexi-quote-rotator/flexi-quote-rotator.php";i:10;s:41:"formilla-live-chat/formilla-live-chat.php";i:11;s:50:"google-analytics-for-wordpress/googleanalytics.php";i:12;s:36:"google-sitemap-generator/sitemap.php";i:13;s:17:"iframe/iframe.php";i:14;s:51:"links-with-icons-widget/links-with-icons-widget.php";i:15;s:31:"seo-wordpress/seo-wordpress.php";i:16;s:27:"shareaholic/shareaholic.php";i:17;s:39:"simple-pull-quote/simple-pull-quote.php";i:18;s:33:"w3-total-cache/w3-total-cache.php";i:19;s:19:"wordcents/index.php";i:20;s:28:"wplegalpages/legal-pages.php";i:21;s:13:"xslt/xslt.php";}
option_value should not be there and I have no idea where or why its happening in my script. I'm guessing the issue lies somewhere in wp_sql but I can't see it. Help is appreciated.
also, ignore the $cBlue and $cDone variables. Those variables just hold bash color codes else where in my script.
This post was edited by SelfTaught on Nov 27 2014 05:12pm