击模板:如何从模板生成配置文件使用bash? [英] Bash Templating: How to build configuration files from templates with Bash?

查看:217
本文介绍了击模板:如何从模板生成配置文件使用bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个脚本来为我自己的网络服务器自动为Apache和PHP创建配置文件。我不希望用这样的cPanel或ISPConfig任何图形用户界面。

I'm writing a script to automate creating configuration files for Apache and PHP for my own webserver. I don't want to use any GUIs like CPanel or ISPConfig.

我的Apache和PHP配置文件的一些模板。 bash脚本需要读取的模板,使变量替换和输出解析模板到某个文件夹。什么是做到这一点的最好办法?我能想到的几种方法。哪一个是最好的,也可以是有一些更好的方式来做到这一点?我想这样做,在纯巴什(很容易在PHP为例)

I have some templates of Apache and PHP configuration files. Bash script needs to read templates, make variable substitution and output parsed templates into some folder. What is the best way to do that? I can think of several ways. Which one is the best or may be there are some better ways to do that? I want to do that in pure Bash (it's easy in PHP for example)

1)如何在一个文本文件替换$ {}占位符?

template.txt:

template.txt:

the number is ${i}
the word is ${word}

script.sh:

script.sh:

#!/bin/sh

#set variables
i=1
word="dog"
#read in template one line at the time, and replace variables
#(more natural (and efficient) way, thanks to Jonathan Leffler)
while read line
do
    eval echo "$line"
done < "./template.txt"

顺便说一句,我怎么输出重定向到这里外部文件?我需要逃避的东西,如果变量包含,比如说,报价?

BTW, how do I redirect output to external file here? Do I need to escape something if variables contain, say, quotes?

2)使用cat&安培; SED与它的值替换每个变量:

2) Using cat & sed for replacing each variable with its value:

由于template.txt:

Given template.txt:

The number is ${i}
The word is ${word}

命令:

cat template.txt | sed -e "s/\${i}/1/" | sed -e "s/\${word}/dog/"

好像对我不好,因为需要逃脱许多不同的符号,并与许多变数行会tooooo长。

Seems bad to me because of the need to escape many different symbols and with many variables the line will be tooooo long.

您可以想到一些其他的优雅和安全的解决方案?

Can you think of some other elegant and safe solution?

推荐答案

您可以使用此:

perl -p -i -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < template.txt

替换所有 $ {...} 与相应的环境变量(不要忘了运行此脚本之前将它们导出)。字符串

to replace all ${...} strings with corresponding enviroment variables (do not forget to export them before running this script).

对于纯的bash这应该工作(假设变量不包含$ {...}字符串):

For pure bash this should work (assuming that variables do not contain ${...} strings):

#!/bin/bash
while read -r line ; do
    while [[ "$line" =~ (\$\{[a-zA-Z_][a-zA-Z_0-9]*\}) ]] ; do
        LHS=${BASH_REMATCH[1]}
        RHS="$(eval echo "\"$LHS\"")"
        line=${line//$LHS/$RHS}
    done
    echo "$line"
done

。不,如果RHS挂解决方案引用一些变量引用本身:

. Solution that does not hang if RHS references some variable that references itself:


#!/bin/bash
line="$(cat; echo -n a)"
end_offset=${#line}
while [[ "${line:0:$end_offset}" =~ (.*)(\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})(.*) ]] ; do
    PRE="${BASH_REMATCH[1]}"
    POST="${BASH_REMATCH[4]}${line:$end_offset:${#line}}"
    VARNAME="${BASH_REMATCH[3]}"
    eval 'VARVAL="$'$VARNAME'"'
    line="$PRE$VARVAL$POST"
    end_offset=${#PRE}
done
echo -n "${line:0:-1}"

警告:我不知道怎样正确处理与完全无效的输入在bash或preserve尾随换行的数量。最后变种psented $ P $,因为它是因为弹爱二进制输入:

WARNING: I do not know a way to correctly handle input with NULs in bash or preserve the amount of trailing newlines. Last variant is presented as it is because shells "love" binary input:


  1. 将间preT反斜杠。

  2. 读-r 不会间preT反斜杠,但还是会下降的最后一行,如果它不以新行结束。

  3. $(...)将剥离尽可能多的尾随换行符有present,所以我最终 ... ;回声-na ,并使用回声-n$ {行:0:-1}:这滴的最后一个字符(即 A )和preserves许多尾随换行符有输入(包括无)。

  1. read will interpret backslashes.
  2. read -r will not interpret backslashes, but still will drop the last line if it does not end with a newline.
  3. "$(…)" will strip as many trailing newlines as there are present, so I end with ; echo -n a and use echo -n "${line:0:-1}": this drops the last character (which is a) and preserves as many trailing newlines as there was in the input (including no).

这篇关于击模板:如何从模板生成配置文件使用bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆