0

I write bash cat << 'EOF' > script to create files.script as below:

#!/bin/bash
for sitename in (site1,site2,site3)
    do
    cat << 'EOF' >/home/$sitename/conf
DEPLOY_DIR="/var/www/$sitename"
git --work-tree="DEPLOY_DIR"
EOF
    done

The result after run this script should like:

[root@localhost]cat home/site1/conf
DEPLOY_DIR="/var/www/site1"
git --work-tree="$DEPLOY_DIR"

The key is I need to substitute $sitename in DEPLOY_DIR="/var/www/$sitename" and keep git --work-tree="DEPLOY_DIR" as same.
How to do it?

kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • @Inian,if I use `<< EOF`,`"$DEPLOY_DIR"` will become `""` – kittygirl Jan 30 '19 at 07:03
  • I added a new answer to the duplicate explaining how to avoid expansion of some strings. Also the `for` loop looks wrong here -- you want `for sitename in site1 site2 site3; do` – tripleee Jan 30 '19 at 07:07

1 Answers1

4
#!/bin/bash
for sitename in site1 site2 site3;do
    cat << EOF > /home/$sitename/conf
    DEPLOY_DIR="/var/www/$sitename"
    git --work-tree="\$DEPLOY_DIR"
EOF
done
NiteRain
  • 663
  • 8
  • 14
  • That's workable, but if you have lots of `$variables` , hard work. – kittygirl Jan 30 '19 at 07:09
  • This is the only way you will be able to extrapolate some variables while not extrapolating others. The other way would require more typing. – NiteRain Jan 30 '19 at 07:11