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

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.