bash - 如何创建一个包含变量的配置文件bash脚本
问题描述
我正在学习 BASH 并尝试创建一个脚本来生成我想在其中使用变量的配置文件。但是,当然,就像这是一个文本文件一样,变量 ${MONGO_PORT} 不会被我需要的值替换。
(
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
第一个问题是关于创建文件的权限,我通过在 /tmp 中创建文件并将其移动到 /etc 来避免。因为这行不通。
sudo cat << MONGOD_CONF
第二个也是更重要的是关于如何用值替换 de 变量
有什么办法可以使这项工作?
谢谢!
解决方案
如果MONGO_PART
设置了,here-document 会受到参数扩展的影响,您将在文件中获得参数值。
使用tee
而不是cat
,它可以打开输出文件本身:
sudo tee /etc/mongod.conf > /dev/null << MONGOD_CONF
...
MONGOD_CONF
推荐阅读
- python - 通过 3d 列表打印特定项目
- javascript - 有没有办法减少动态创建的每个孩子的边距,这样孩子就不会溢出父母
- python-3.x - 如何在treeview PyQt5中删除父项的子项
- python - Python:从文本文件的一行中删除一个字符串 python
- java - java.lang.OutOfMemoryError 尝试实现极小极大算法时
- c++ - _getcwd 不是返回当前工作目录,而是返回程序存储的地方
- angular - Angular - 为什么在分离的组件上调用生命周期挂钩?
- sql - 使用 SSIS 数据包运行作业时触发插入日期失败
- python - Python + Plotly:如何绘制具有多种宽度的线条
- c++ - 向数组添加索引会导致 C++ 中的数组移位