首页 > 解决方案 > 如何创建一个包含变量的配置文件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

有什么办法可以使这项工作?

谢谢!

标签: bashvariablesconfiguration-files

解决方案


如果MONGO_PART设置了,here-document 会受到参数扩展的影响,您将在文件中获得参数值。

使用tee而不是cat,它可以打开输出文件本身:

sudo tee /etc/mongod.conf > /dev/null << MONGOD_CONF
    ...
MONGOD_CONF

推荐阅读