0

I am trying to read wp usernames and passwords / db names from my script - however I am facing the below issue;

wpconfigs=($(find . -name "wp-config.php"))
for i in "${wpconfigs[@]}"; do
    wpdb=$(grep -e "DB_NAME" $i | cut -d \' -f 4)
    wpuser=$(grep -e "DB_USER" $i | cut -d \' -f 4)
    wppass=$(grep -e "DB_PASS" $i | cut -d \' -f 4)
    allpriviledges="ALL PRIVILEGES"

If I echo the results for example if DB name was 12345

I'd get

wpdb=12345

If the username has a special char, for example a $ I get

wpdb='12434$'

The result is showing '' either side if a special char is found

However, as I am passing the result to a command - the command recieves '123434$' where a special char is found (wrong) and works where it is 12345 as it says wpdb=12345 rather than wpdb='12345$'

I am passing the results to the below command;

So where it isn't a special char it works fine

uapi --output=jsonpretty --user="$cpuser" Mysql create_user name="${wpuser}" password="${wppass}";
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The code isn't adding the quotes, they must be in the file. Can you show an example of `wp-config.php`? – Barmar Feb 04 '23 at 20:30
  • 3
    Run this through http://shellcheck.net. – chepner Feb 04 '23 at 20:33
  • 2
    Doesn't make sense that `cut` will allow the delimiter to be printed. Please give a example `DB_PASS` entry that makes that happen. – konsolebox Feb 04 '23 at 20:39
  • 2
    Also please explain how you found out `''` was added on either side and that it's not simply a quoted output like maybe from `uapi`. – konsolebox Feb 04 '23 at 21:01
  • 2
    This looks like a followup to your earlier question, ["For-Loop strange issue"](https://stackoverflow.com/questions/75344977/for-loop-strange-issue), where it looks like you're getting very confused about how to use `set -x` and how to interpret its output. As pjh pointed out in comments there, the single-quotes are an artifact of how `set -x` shows parameters that contain funny characters like `$`. They are *not* part of the actual parameter, they're just added to indicate that the funny character(s) are literally part of the parameter, rather than having their normal special meaning. – Gordon Davisson Feb 04 '23 at 21:38
  • 2
    See ["Why does bash script add single quotes to string with asterisk?"](https://stackoverflow.com/questions/42561463/why-does-bash-script-add-single-quotes-to-string-with-asterisk) for a similar situation. – Gordon Davisson Feb 04 '23 at 21:42
  • 1
    Consider writing your code in php. Just use the wordpress config php file as is in php, instead of monkey parsing it with grep. – KamilCuk Feb 04 '23 at 22:05
  • This is just a guess, but I think it's possible that the dollar in the username is causing problems for MySQL. See [How to escape dollar sign in mysql when executed by command line?](https://stackoverflow.com/q/27415371/4154375) for an example of a problem caused by a dollar sign in a different context. If strings do need additional quoting before passing them to `uapi`, you probably don't want to be doing that with Bash code. The suggestion by KamilCuk to write your code in PHP seems like a good idea. – pjh Feb 05 '23 at 00:18
  • This means that the value is written in exactly this way in the PHP file. You would have to post here all lines in your PHP program, which contain the string `DB_NAME` (i.e. post the output of `grep DB_NAME $i`) – user1934428 Feb 05 '23 at 07:18

0 Answers0