我有一个文件模板,将被shell脚本用来生成其他文件,只是使用variables来更改此文件模板中的填充信息。
文件template.example.log内容:
server name: $HOSTNAME current user: $USER
我尝试不同的方式来做这个作品。
文件log.sh内容:
1)不起作用,输出文件some_log_1.log与template.example.log相同,variables不变。
FILE_TEMPLATE="`cat template.example.log`" cat > some_log_1.log <<EOF $FILE_TEMPLATE EOF
2)也不要使用echo命令,与上面发生的一样。
echo $FILE_TEMPLATE > some_log_1.log
但如果我这样做,它的作品!
cat > some_log_1.log <<EOF server name: $HOSTNAME current user: $USER EOF
输出是正确的!
server name: ubuntu-server current user: leafar
但我不希望脚本以这种方式运行。 我想读取一个文本文件的内容,并将其embedded到另一个文本文件中并fill the variables 。
我怎样才能做到这一点? 谢谢。 诗:对我弱的英语感到抱歉
一个选项是使用sed编辑文件。 这可以让你限制什么variables填写。
这将填充template.txt并将其输出到filled.txt:
sed -e "s/\$HOSTNAME/$HOSTNAME/" -e "s/\$USER/$USER/" template.txt > filled.txt
template.txt:
server name: $HOSTNAME current user: $USER
filled.txt:
server name: ubuntu-server current user: leafar