d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Bash Script
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Nov 27 2014 05:11pm
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
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 27 2014 08:00pm
i write a few shell scripts as needed on my mac, but it's a real pain. i just use python if i want anything more complicated then running basic commands. i dont even wanna read that mess :/

This post was edited by carteblanche on Nov 27 2014 08:01pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Nov 27 2014 09:07pm
Quote (carteblanche @ Nov 27 2014 06:00pm)
i write a few shell scripts as needed on my mac, but it's a real pain. i just use python if i want anything more complicated then running basic commands. i dont even wanna read that mess :/


xD

yeah this is only a small part of the script too. i'd only used bash via cli before writing this so this is some what of a learning experience and is probably pretty terrible bash scripting.

it might be a good idea to just rewrite this whole thing in perl. the reason why i didn't in the first place is because i can't have a physical file to execute in the environment i'm working in. the script has to be either injected into an ssh session (which is very convenient to do w/ bash) or has to be directed into a local interpreter from a remote location. the later could be done with perl but i didn't think about it until i had started writing this terrible mess.

This post was edited by SelfTaught on Nov 27 2014 09:08pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 27 2014 09:11pm
Quote (SelfTaught @ Nov 27 2014 10:07pm)
xD

yeah this is only a small part of the script too. i'd only used bash via cli before writing this so this is some what of a learning experience and is probably pretty terrible bash scripting.

it might be a good idea to just rewrite this whole thing in perl. the reason why i didn't in the first place is because i can't have a physical file to execute in the environment i'm working in. the script has to be either injected into an ssh session (which is very convenient to do w/ bash) or has to be directed into a interpreter. the later could be done with perl but i didn't think about it until i had started writing this terrible mess.


i keep forgetting what it's called. there's some feature where you can make a section of your file be treated as a separate file. home document? something like that. so you can write your perl script on top, then as the last line you treat the above as a file and you execute it like a script.

/edit: here document! http://en.wikipedia.org/wiki/Here_document

This post was edited by carteblanche on Nov 27 2014 09:13pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Nov 27 2014 09:34pm
Quote (carteblanche @ Nov 27 2014 07:11pm)
i keep forgetting what it's called. there's some feature where you can make a section of your file be treated as a separate file. home document? something like that. so you can write your perl script on top, then as the last line you treat the above as a file and you execute it like a script.

/edit: here document! http://en.wikipedia.org/wiki/Here_document


ah yeah. heredocs are useful but afaik the code would still reside in a file somewhere in the environment. i use my own scripts at work to make troubleshooting problems on customer accounts more convenient so i can't have a single line of code left behind :\ if i use perl what ill probably do is have my script located on a remote host then execute it locally like so:

Code
perl <(curl -S http://remotehost.com/script.pl) -option


thats pretty similar to how im currently doing it in bash except with perl, i'd have to curl the remote script every time i wanted to execute something in it. with bash, you can just curl it once and have the script remain in the session. then all you have to do is type the function name that you want to execute inside of the script

Code
. <(curl -Ss http://remotehost.com/script.sh)


i wonder if python code can be directed into its interpreter and executed in a similar fashion o_0
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 27 2014 09:42pm
Quote (SelfTaught @ Nov 27 2014 10:34pm)
ah yeah. heredocs are useful but afaik the code would still reside in a file somewhere in the environment. i use my own scripts at work to make troubleshooting problems on customer accounts more convenient so i can't have a single line of code left behind :\ if i use perl what ill probably do is have my script located on a remote host then execute it locally like so:

Code
perl <(curl -S http://remotehost.com/script.pl) -option


thats pretty similar to how im currently doing it in bash except with perl, i'd have to curl the remote script every time i wanted to execute something in it. with bash, you can just curl it once and have the script remain in the session. then all you have to do is type the function name that you want to execute inside of the script

Code
. <(curl -Ss http://remotehost.com/script.sh)


i wonder if python code can be directed into its interpreter and executed in a similar fashion o_0


i was thinking you can use a here document to put python/perl inside your .sh file and make up 90% of it, and add a few simple bash functions on the bottom which just call the python above. then you curl it once and call your bash functions.

just a thought, idk if it works lol
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Nov 27 2014 10:55pm
Quote (carteblanche @ Nov 27 2014 07:42pm)
i was thinking you can use a here document to put python/perl inside your .sh file and make up 90% of it, and add a few simple bash functions on the bottom which just call the python above. then you curl it once and call your bash functions.

just a thought, idk if it works lol


Oh, I see what you're saying now. Hm.. interesting. ill play around with that and see if it will work :D

This post was edited by SelfTaught on Nov 27 2014 10:56pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll