Due to a problem from a netizen, I sorted it out ......
--------------------------------------------
Generally, I like to help others solve problems and feel a sense of accomplishment. Is this idea a bit ...... Haha, that's what I do. I prefer to look at other people's problems and try my best to help him. Therefore, many netizens often ask me some questions. After a long time, there will be more problems. I think we should accumulate this thing. So I want to publish it in the form of a blog. On the one hand, I may forget to come back later. On the other hand, I hope to help more friends.
My opinion is strong. If you have different opinions, you can leave a comment. Do not spray it! Thank you ~
Let's talk about a question from a group of netizens;
A configuration file wp-config.php under the web server, there are several subdirectories under the/var/www/directory, the content is also different, and now want to modify these configuration files all have a line
/** MySQLdatabasepassword */
Define ('db _ password', 'ggev! & C2rsDsdfEWayAz ');
Change the password of the database DB_PASSWORD in the following single quotes to the specified string.
Yes. All files named wp-config.php under the/var/www/directory must be modified.
Well, I encountered this problem. As a cainiao, I followed three basic principles under the guidance of my teacher;
1. Create a test file first;
2. Test Data Processing;
3. Truly process files;
Okay. Open the Virtual Machine. CentOS6.4 system.
[root@Jason64-17 ~]# cat /etc/redhat-releaseCentOS release 6.4 (Final)
View the created test file;
[root@Jason64-17 ~]# ls wp-config -l-rw-r--r-- 1 root root 32 Oct 8 14:02 wp-config
Create a batch copy script;
I am not very satisfied with the batch copy scripts I have written. If you have any good ideas, you can send them. Xi ~]
[root@Jason64-17 ~]# cat cp.sh#!/bin/bash#program# this program help you copy file to wherever you want#history#09/28/13 lisp first release#read -p "Please input filename i will cp it to everywhere: " filename[ "$filename" == "" ] && echo "you must input filename! " && exit 1for i in $@docp $filename $idone
Copy it to multiple directories in batches
[root@Jason64-17 ~]# sh cp.sh /var/www/html/ /var/www/ /var/www/2/Please input filename i will cp it to everywhere: wp-config.php[root@Jason64-17 ~]# find /var/www/ -type f -name wp-config.php/var/www/html/wp-config.php/var/www/2/wp-config.php/var/www/wp-config.php
Test Data Processing
View the data to be processed;
[root@Jason64-17 ~]# nl wp-config.php1 <?php2 /**3 * The base configurations of the WordPress.4 *……omitting……21 /** MySQL database password */22 define('DB_PASSWORD', 'GgEv!&C2rsDsdfEWayAz');……omitting……
Use sed for data processing testing;
[root@Jason64-17 ~]# sed -n 's/\(PASSWORD....\).*\(..;\)/\1123456\2/gp' wp-config.phpdefine('DB_PASSWORD', '123456');
Or
[root@Jason64-17 ~]# sed -n "s/PASSWORD', '.*'/PASSWORD', '123456'/gp" wp-config.phpdefine('DB_PASSWORD', '123456');
Here we want to talk to you about using sed. We usually use hard quote to put it on the two shoulders of sg. However, if the content we want to replace contains hard quote) what about it?
There are two ways (I think of it now): one is to avoid it, and the other is to directly "face" it.
The first method is to escape. We use \ (\) to replace it with a single quotation mark, so there is nothing to say about it.
The second method is to use double quotation marks (softquote) outside, so that the single quotation marks inside you can be unobstructed .......
Why can't single quotes be used again in single quotes?
In my understanding, when the sed statement is executed, the condition symbol for determining the start of execution is single quotation marks, the single quotation mark that is encountered again in the execution process will be processed as the end (escape is useless at all). If the single quotation mark is encountered again, it will be judged as the start, so when you write the statement, you can try the Tab key when writing the source file of the subsequent data stream. It does not respond because sed has not finished the mark, so it is not a shell command by default, so Tab is useless. Only when you write the complete start and end identifiers will you complete the file name when you Tab.
PS: this can also be used to determine whether your sed statement is correctly written. If you press the Tab for half a day and there is no completion function, it indicates that your command is faulty; so does awk!]
Well, after we have completed the key step, we will have a piece of cake next!
Use find to find the file to be modified
[root@Jason64-17 ~]# find /var/www/ -type f -name wp-config.php/var/www/html/wp-config.php/var/www/2/wp-config.php/var/www/wp-config.php
This is actually the command I just executed. Execute it again to check which files are available)
Use the sed data processing function in combination with the pipeline to modify the test link for the found files]
[root@Jason64-17 ~]# find /var/www/ -type f -name wp-config.php | xargs sed -n "s/PASSWORD', '.*'/PASSWORD', '123456'/gp"define('DB_PASSWORD', '123456');define('DB_PASSWORD', '123456');define('DB_PASSWORD', '123456');[root@Jason64-17 ~]#
If you are satisfied with the test results, use the sed-I parameter to directly modify the file;
[root@Jason64-17 ~]# find /var/www/ -type f -name wp-config.php | xargs sed -i "s/PASSWORD', '.*'/PASSWORD', '123456'/g"[root@Jason64-17 ~]# find /var/www/ -type f -name wp-config.php | xargs grep PASSWORD/var/www/html/wp-config.php:define('DB_PASSWORD', '123456');/var/www/2/wp-config.php:define('DB_PASSWORD', '123456');/var/www/wp-config.php:define('DB_PASSWORD', '123456');[root@Jason64-17 ~]#
Pay attention to the use of xargs. Some may ask: does grep and sed support pipelines? How can I add xargs?
If xargs is not added, both sed and grep process the searched file name, rather than the content in the file. After xargs is added, xargs will pass the result of the previous command as parameters to the next command in sequence. Of course, some commands do not support pipelines, so you need to add xargs for processing.
Now, the problem is solved.
To solve this problem, I used a lot of knowledge, including find, sed, grep, xargs, and shell scripts. Great gains ~ Haha!
I also hope you can give me more suggestions. If this article is not well written, I still forget to correct it.
By the way, if you do not understand the usage of some of the above commands, you can search for information on your own, and chat with me on QQ. If you are free, I will reply to you in time; My QQ: 1031239088.
In short, I want everyone to make progress together!
This article from the "lisp O & M path" blog, please be sure to keep this source http://lspgyy.blog.51cto.com/5264172/1303255