下面是一个使用BASH脚本填充模板文件字符串的解决方法:
#!/bin/bash
template_file="template.txt"
output_file="output.txt"
# 定义模板中的占位符
placeholder1="{{NAME}}"
placeholder2="{{AGE}}"
# 定义要替换的值
name="John Doe"
age="30"
# 读取模板文件内容
template=$(cat "$template_file")
# 替换占位符为实际值
content=${template//$placeholder1/$name}
content=${content//$placeholder2/$age}
# 将替换后的内容写入输出文件
echo "$content" > "$output_file"
其中,template.txt
是包含占位符的模板文件,output.txt
是生成的输出文件。在脚本中,通过定义模板中的占位符以及实际要替换的值,然后使用字符串替换功能${template//$placeholder1/$name}
将占位符替换为实际值。最后将替换后的内容写入输出文件。
请注意将template.txt
中的占位符{{NAME}}
和{{AGE}}
替换为实际的占位符,在脚本中定义的占位符要与模板文件中的占位符一致。