Before using shell to write scripts, sometimes not only to define a configuration file, often have a template, different environments directly replace the same template content to use, however, until then, has been used in the SED, awk, grep and so on. Suddenly one day there is a Yaml file, the inside to replace too much, sed too troublesome, with this method, do not know how much to write less regular ...
Not much to pull, I give the big guy an example:
Create a template to write variables directly within the template
Vim A.model
#This is app config model
App_name= $name
app_ip= $ip
Shell scripts Replace variables directly
Vim b.sh
#!/bin/bash
Name= "Bill_server"
ip= "192.168.3.22"
all_variables= "Name= $name ip= $ip"
Cp-a A.model rfile.conf
resourcefile=cat rfile.conf
printf "$all _variables\ncat << eof\n$resourcefile\neof" | Bash > rfile.conf
After running the script, review the generated configuration file:
Cat rfile.conf
#This is app config model
App_name=bill_server
app_ip=192.168.3.22
Isn't it convenient? If there are many environments with the same configuration file, using this method to do configuration files, is not very convenient, as long as the shell variables from the outside to the parameter, copy of the file is also hungry to change the variable, you can generate a lot of the same format content different files. If this configuration file is long enough, how much is it saved?
Shell defines a template with variables, directly modify the variables, in this way no longer have to worry about the regular match is not allowed to the problem