I'm learning BASH and trying to create a script that generates a configuration file in which I want to use variables. But of course, like this is a text file, the variable ${MONGO_PORT} is not being replaced by the value i need.
(
cat << MONGOD_CONF
# /etc/mongod.conf
systemLog:
destination: file
path: /datos/log/mongod.log
logAppend: true
storage:
dbPath: /datos/db
engine: wiredTiger
net:
port: ${MONGO_PORT}
security:
authorization: enabled
MONGOD_CONF
) > /etc/mongod.conf
The first problem is about permission to create the file that I'm avoiding by creating the file in /tmp and moving it to /etc. Because this doesn't work.
sudo cat << MONGOD_CONF
The second and more important is about how to replace de variable by the value
Any way to make this work?
Thank you!
If MONGO_PART
is set, the here-document is subject to parameter expansion and you'll get the parameter value in your file.
Use tee
instead of cat
, which can open the output file itself:
sudo tee /etc/mongod.conf > /dev/null << MONGOD_CONF
...
MONGOD_CONF