Use sed to explain and use sed to explain
I am a bit confused about this title. This article only describes a problem I encountered in practice, the cause of the problem, and the solution. in addition, experienced people may be able to see the problem at a glance. at least know about debugging and troubleshooting.
Unfortunately, I actually know that debugging takes more than an hour. In many cases, what we lack is just a habit.
Let's talk about the problem first. Let's first look at the script below.
#!/bin/bashif test $# -lt 1then echo -e "uasge : must have a args\n" exitfiif test ! -d $1then echo -e "usage : must be dir\n" exitfifor i in `ls $1/*.xml`do sed -i "s/\(.*shell\).*\(\".*\)/\1\/$i\2/gp" test.xmldone
The content of test. xml is as follows:
<?xml version="1.0" encoding="UTF-8"?><test version="2.0.1"> <person name="alai" sex="nan" home="/home/alai/testspace/shell/sed/test.xml" /> </test>
This script is used to find the file in another folder and replace sed/test. xml in the test. xml file with the xml file in other folders.
If this script runs independently, it is no problem to replace the iron with another character string such as alai.txt:
However, the running result in the script is as follows.
This error indicates that the content in the regular expression is incorrect, but the specific problem is unknown.
I am ashamed that I have taken many detours. at first, I thought sed had a problem accessing the shell variable. I found no problem after reading some documents. later, the version does not match. however, the solution to the problem is ineffective.
In desperation, I tried sh-x. This command is used to execute shell scripts through debugging.
Sh-x:
Careful students may have seen it. test/test is displayed in double quotation marks. xml statements. we know that shell is text interpreted. that is to say, he translates every sentence of code and executes it. when it is translated into the wrong sentence. $ I is expanded. test/test. xml. in this expression, '/' has a special meaning. It exists as a separator.
Since the problem is found, it is not so troublesome to solve it.
Two solutions:
1. Prevent the/. Modified script from appearing in double quotes as follows:
#!/bin/bashif test $# -lt 1then echo -e "uasge : must have a args\n" exitfiif test ! -d $1then echo -e "usage : must be dir\n" exitfifor i in `(cd $1;ls *.xml)`do sed -n "s/\(.*shell\).*\(\".*\)/\1\/$1\/$i\2/gp" test.xmldone
2. Replace the separator with other characters that are not present in the expression:
#!/bin/bashif test $# -lt 1then echo -e "uasge : must have a args\n" exitfiif test ! -d $1then echo -e "usage : must be dir\n" exitfifor i in `ls $1/*.xml`do sed -n "s+\(.*shell\).*\(\".*\)+\1\/$i\2+gp" test.xmldone
Let's make a brief conclusion. I'm glad I didn't rely on others' help and online ideas. It seems that there are no similar problems on the Internet, and it may be too simple.
No matter what the problem is for others, it is an expansion of thinking for me and I hope that the text will inspire my students.