1

While in /var/www/html I executed the following cat heredocument which serves as a maker - It makes the file /opt/dwa.sh which includes a function and a MYSQL heredocument.

cat <<-"DWA_INSTALL" > /opt/dwa.sh
    #!/bin/bash
    DWA() {
        test='test'

        read domain
        cp -rv /var/www/html/${domain} /var/www/html/${test}
        sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        mysql --force -u root -p <<-MYSQL
            CREATE user '${test}'@'localhost' IDENTIFIED BY '${psw}';
            CREATE database ${test};
            GRANT ALL PRIVILEGES ON ${test}.* TO ${test}@localhost;
        MYSQL
    }
    DWA
DWA_INSTALL

After making the file in execution, I executed the file itself which contains the function, passing a ${domain} argument to read.

Let's assume that this argument was:

example.com

My problem

For testing purposes, I then rexecuted the cat heredocument maker, but now, instead getting this content inside dwa.sh:

#!/bin/bash
    DWA() {
        test='test'

        read domain
        cp -rv /var/www/html/${domain} /var/www/html/${test}
        sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        mysql --force -u root -p <<-MYSQL
            CREATE user '${test}'@'localhost' IDENTIFIED BY '${psw}';
            CREATE database ${test};
            GRANT ALL PRIVILEGES ON ${test}.* TO ${test}@localhost;
        MYSQL
    }
DWA

I got this, inside it:

    html/example.com/#!/bin/bash
    html/example.com/html/example.com/DWA() {
        html/example.com/test='test'
        html/example.com/
        html/example.com/read domain
        html/example.com/cp -rv /var/www/html/${domain} /var/www/html/${test}
        html/example.com/sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        html/example.com/sed -i 's/${domain}/${test}'/g /var/www/html/test/wp-config.php
        html/example.com/mysql --force -u root -p <<-MYSQL
            html/example.com/CREATE user '${test}'@'localhost' IDENTIFIED BY '${psw}';
            html/example.com/CREATE database ${test};
            html/example.com/GRANT ALL PRIVILEGES ON ${test}.* TO ${test}@localhost;
        html/example.com/MYSQL
    html/example.com/}
html/example.com/DWA

My question

From where these html/example.com strings come from? I have a feeling it has to do with read somehow, like it is some variable's value got "stuck" in the memory, but I don't know how to track down the variable by it's value and have no clue how to find out the source of these strings.

I tried to do domain='' and re-execute the cat heredocument again, but still the same result comes out. Given the cat heredocument doesn't not allow variable expansion due to double quotes ("EOT"), I feel even weirder with this.

I am quite new to Bash and don't have much experience working with variables (was never really part of the courses I took). Maybe you can advice.

Update --- I solved the problem without understanding how:

After executing in console exec bash, I tried to repeat the regular pattern of running DWA_INSTALL and this time the file was created without the problem of the html/example.com. I don't know why, but it helped.

closed as unclear what you're asking by Michael Homer, Stephen Rauch, GAD3R, ilkkachu, Rui F Ribeiro Dec 4 '17 at 9:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • So, you ran the cat <<-"DWA_INSTALL" command twice, and got different output? Can you repeat that? Produce a minimal example illustrating the issue? – Michael Homer Dec 3 '17 at 3:25
  • I ran it a few times actually. I don't know what went wrong in the middle. I am looking for any tip, any direction, that will help me understand what can cause the output I get now, each time I execute cat <<-"DWA_INSTALL". – Arcticooling Dec 3 '17 at 3:31
  • (1) I’m confused. You say you have done this several times.  You describe three actions: (A) the cat, (B) running the script, and (C) looking at the script.  Have you ever done A and then C without doing B?  Does the file still have html/example.com/ on every line?  If so, it has nothing to do with the fact that you provided an input of example.com in step B.  If not, explain what you have done better. … (Cont’d) – G-Man Dec 3 '17 at 8:00
  • (Cont’d) …  (2) Try this: cat <<- "EOF" (without redirecting output), then type some random lines, and then EOF. Include in the input at least one line starting with each of these strings: (i) a letter, (ii) a #, (iii) a }, (iv) a space, (v) a tab, and (vi) two tabs. Post the output. (3) Do you have a file or directory called example.com? Perhaps in an html directory? (4) Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Dec 3 '17 at 8:00
  • G-Man, I didn't try only A and C without B. Anyway, here's an update, after executing exec bash and than doing everything anew again --- I no longer had the problem described in the question. – Arcticooling Dec 3 '17 at 18:23
0

I solved the problem without understanding how:

To clear all non environment variables (that is, all variables I myself declared), I ran in console:

exec bash

I hen tried to repeat the regular pattern of running DWA_INSTALL and this time the file was created without the problem of the html/example.com preliminaries.

I don't know why, but it helped; Please edit this answer with an explanation if you figured out what happened there.

Not the answer you're looking for? Browse other questions tagged or ask your own question.