First shell Script--Modify configuration file

Source: Internet
Author: User

There is demand, can let oneself lazy is the real motive force of study. Because the test environment always needs to manually modify the configuration file after building the code, the learning side completes a shell script that can be modified with one click.

Defines a function that displays the output information in a green font.

function Echo_green {        "\033[32m$1\033[0m"}

Output hint information

" do you want to modify XXX with one click? (y/n) "

Reading keyboard input

Read answer

There are two types of configuration files to modify: One is clearly clear which strings should be modified, directly with the substitution string, if it is/home/db.conf.php. The other is not familiar with the configuration file, modified once after the file is directly saved, each time you build the code with the modified file to overwrite, if it is/home/hosts. This is not recommended and may cause unnecessary hassles when new changes are made to the file that could be overwritten and not be effective.

The input is determined first, and the input is y when modified

if " $answer " " y "  Then

1. Modify the string

sed " s/replaced string/replaced with string/g "  "db.conf.php modified "

Tips

The ①sed-i:sed command adds-I to directly manipulate the original file

②/g are all overrides, otherwise only the first of each line is substituted.

2. Replacing files

If we put the modified documents in/home/ready/hosts

File attributes and file owner and user groups need to be modified after overwriting

CP /home/ready/hosts/home/hostssudochmod755 /home/hosts sudo chown deploy:deploy/home/"hosts modified "

After all the modifications are complete, the output message

Echo_green "All Modified"

If the keyboard input information is not y, output a hint, exit

else" bye, you "exitfi

Full code

functionEcho_green {Echo-E"\033[32m$1\033[0m"}echo_green"do you want to modify db.conf.php, hosts with one click? (y/n)"Read Answerif["$answer"=="y"]; Then    sed-I."s/replaced string/replaced with string/g"/home/db.conf.php Echo_green"db.conf.php has been modified"        CP/home/ready/hosts/home/hostssudo chmod 755/home/hostssudo Chowndeploy:deploy/home/hosts Echo_green"The hosts have been modified"Echo_green"All modified"    ElseEcho_green"bye, you."Exitfi



First shell Script--Modify configuration file

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.