Raphael
Raphael

Reputation: 1909

Linux bash - dynamic config file

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!

Upvotes: 1

Views: 1206

Answers (1)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185025

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

Upvotes: 1

Related Questions