1

I try to combine two bash files to create a dynamic configuration file. One of these files (settings.sh) contains variables and the other (config.sh) commands to execute on them.

For instance, in settings.sh I have

DATABASE='name_of_the_db'
USER='name_of_the_user'

and config.sh contains

. settings.sh
[...]
'NAME': '$DATABASE',
'USER': '$USER',
[...]

I'm struggling to create a command that would get the variable in the first file and insert it in the second, rendering a new file (settings.py) containing

'NAME': 'name_of_the_db',
'USER': 'name_of_the_user',

I tried a command like

cat < settings.py [...] EOF

as proposed here How does ` cat << EOF` work in bash? but this does obviously not perform any variable replacement.

Thank you in advance!

Community
  • 1
  • 1
Raphael
  • 1,709
  • 2
  • 17
  • 27

1 Answers1

1

What I would do :

settings.sh is OK

config.sh :

. settings.sh
cat<<EOF
[...]
'NAME': "$DATABASE",
'USER': "$USER",
[...]
EOF

And to create the config.py file :

chmod +x config.sh
./config.sh > config.py

The double quotes in config.sh are mandatory, if you want the variables to be interpolated. See http://mywiki.wooledge.org/BashGuide/Practices#Quoting

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223