【发布时间】:2012-11-15 14:31:59
【问题描述】:
我正在使用 bash 脚本来安装和配置应用程序。我正在尝试使用一个模板,我可以在其中填写变量中的值,然后将其保存到适当的位置。这仅适用于变量,但我无法使用任何条件。有没有更好的方法来做到这一点?
这是我的示例模板:
#!/bin/bash
cat <<EOF
DEFAULTS: &DEFAULTS
adapter: $DB
host: $DB_HOST
username: $DB_USER
password: $DB_PASS
database: $DB_DATABASE
if [ ! $DB_RECONNECT = ""]; then
reconnect: $DB_RECONNECT
fi
if [ ! $DB_TIMEOUT = ""]; then
timeout: $DB_TIMEOUT
fi
EOF
然后我使用source template.sh > /path/to/file 评估并保存文件。
【问题讨论】:
-
想一想发生了什么,您是否希望执行
cated 文件?这就是你做的对吗?除了cat的文件源是由<<EOFhere-document 提供的。那么为什么不按照你的source template > file和chmod 755 file; ./file来执行里面的逻辑呢?
标签: bash templates scripting templating