Home > Software design >  For-Loop strange issue
For-Loop strange issue

Time:02-05

Please see below outputs, it looks like the first line isn't working for the first file only - but is for the others, any ideas?

   #!/bin/sh

cpuser=$1
cd "/home/$cpuser/public_html"

wpconfigs=($(find . -name "wp-config.php"))
for i in "${wpconfigs[@]}";

do
cpuser=$cpuser
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)

set -x uapi --output=jsonpretty --user="$cpuser" Mysql create_user name="${wpuser}" password="${wppass}";
sleep 2s;
set -x uapi --output=jsonpretty --user="$cpuser" Mysql set_privileges_on_database user="${wpuser}" database="${wpdb}" privileges="ALL PRIVILEGES";
sleep 2s;
set -x uapi --output=jsonpretty --user="$cpuser" Mysql set_password user="${wpuser}" password="${wppass}";
sleep 2s;
done

When I look at the output run set -x to debug I see the following;

[root@cpanel201 ~]# sh ./testing.sh qv5goe35p3783sz8
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_privileges_on_database 'user=qv5goe35p3783sz8_yscr_bbS$L5' database=qv5goe35p3783sz8_blog 'privileges=ALLPRIVILEGES'
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_password 'user=qv5goe35p3783sz8_yscr_bbS$L5' password=FhtH6UztIuoS0_
  sleep 2s
  for i in "${wpconfigs[@]}"
  cpuser=qv5goe35p3783sz8
   grep -e DB_NAME ./wordpress/wp-config.php
   cut -d \' -f 4
  wpdb=qv5goe35p3783sz8_wordpress
   grep -e DB_USER ./wordpress/wp-config.php
   cut -d \' -f 4
  wpuser=qv5goe35p3783sz8_yscr_bb5X8h
   grep -e DB_PASS ./wordpress/wp-config.php
   cut -d \' -f 4
  wppass=X7lyC17Td8tH3cm
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql create_user name=qv5goe35p3783sz8_yscr_bb5X8h password=X7lyC17Td8tH3cm
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_privileges_on_database user=qv5goe35p3783sz8_yscr_bb5X8h database=qv5goe35p3783sz8_wordpress 'privileges=ALL PRIVILEGES'
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_password user=qv5goe35p3783sz8_yscr_bb5X8h password=X7lyC17Td8tH3cm
  sleep 2s
  for i in "${wpconfigs[@]}"
  cpuser=qv5goe35p3783sz8
   grep -e DB_NAME ./newsite/wp-config.php
   cut -d \' -f 4
  wpdb=qv5goe35p3783sz8_newsite
   grep -e DB_USER ./newsite/wp-config.php
   cut -d \' -f 4
  wpuser=qv5goe35p3783sz8_yscr_bbxQiN
   grep -e DB_PASS ./newsite/wp-config.php
   cut -d \' -f 4
  wppass=XpMa4v5z54tcM
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql create_user name=qv5goe35p3783sz8_yscr_bbxQiN password=XpMa4v5z54tcM
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_privileges_on_database user=qv5goe35p3783sz8_yscr_bbxQiN database=qv5goe35p3783sz8_newsite 'privileges=ALL PRIVILEGES'
  sleep 2s
  set -x uapi --output=jsonpretty --user=qv5goe35p3783sz8 Mysql set_password user=qv5goe35p3783sz8_yscr_bbxQiN password=XpMa4v5z54tcM
  sleep 2s

If you look at the outputs for the second third set of files you'll see three uapi commands, however, for the first we only see two and no outputs in terms of the greps it is doing on the top file.

For your reference, cpuser is set on the command line (the value after sh ./testing.sh)

I've also noticed that in the top file, the user appears to get surrounded by 's however the other two don't.

For clarity what the script is doing here;

  • Scraping any wp configs for a sql db name, db user and db password
  • Passing the user name and password to a cpanel api to create the user in a cPanel instance

The action works on config files 2 and 3 in this instance, but, not with the initial one - you will see the first create_user is missing even on the set -x

**

UPDATE

Having reviewed the logs, I believe this may be due to the fact that the user on the first one has a $ within it - could this be why? I have noticed that only on this one also, the command gets quoted within ''s which is incorrect for uapi. If this may be why, how do I resolve that?

'user=qv5goe35p3783sz8_yscr_bbS$L5'

How do I escape the $, as I think that's what is causing the issue in this case - all others are okay, only where a $ is within the username does this issue occur

CodePudding user response:

The follow code tells it all:

$ set -x ls
$ ls
  ls
a.out    

As far as I know in all shells the 'ls' behind 'set -x' is ignored. By the next '$ ls' and ' ls' you can see that 'set -x' is executed.

The normal way to do this, and probably what you want, is:

$ set -x 
$ ls
  ls
a.out   
$ set -
  set -  

Or as a one liner:

$ set -x ; ls ; set -
  ls
a.out
  set -    

By the previous you can also see why the line is missing in your output 'set -x' just enables shell tracing for the next command, where 'set -' ends it also for the next command.

  • Related