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