Writing this article when suddenly remembered "crazy Stone" in the film, Xu Zheng does played by the boss dozen of his men, said: "2 million of business by you made 10 million, you still have the nerve to come to me." ”。 Today, I also made such a single, I'm going to vomit, there is the felling of the sewer capsized.
The problem is that you need to display the HTML page of an echo of a script, but because the string contains characters such as "\ n", "T", and so on, the HTML needs to be converted to the appropriate label or escape character, otherwise the display format is incorrect.
The original shell script is like this:
#!/bin/bash
Echo-e "This are a Demo:\tyingoulifanchuan"
echo-e "from the film:\t ' Crazy Stone '" Echo-e
"Hope This can help you: "
The results of the implementation are as follows:
The contents of this script are saved to a variable in another script, and then the variable is sent to the HTML (the business logic is not verbose, irrelevant to the topic), and the effect of the HTML is expected to match the result of bash execution.
Okay, look at the script I started writing.
#! /bin/sh
result=$ (/bin/sh/etc/snmp/original.sh)
errorcount=$ (echo $result | egrep "Crazy stone| Xunzhen "| WC-L)
If [$errorCount-gt 0]; then
echo $result;
Exit/
Else
exit 112
fi
Fully exposed to the innocence of my nature, the page shows up a long slip
There are no tab characters or line breaks. The reason is that in the shell, tabs, newline characters, and spaces are used as the delimiters for the arguments, and only the variable is quoted as a whole.
Add quotes to the $result on both sides of the script, HTML effect as shown
tab characters and line breaks appear as ".".
OK, here's the replacement, replace the newline character with "<br>", the tab is deleted directly, and the script is modified as follows:
#! /bin/sh
result=$ (/bin/sh/etc/snmp/original.sh)
errorcount=$ (echo $result | egrep "Crazy stone| Xunzhen "| WC-L)
If [$errorCount-gt 0]; then
echo "$result" | sed ' s/\n/<br>/g ' | sed ' s/\t//g '
exit
el Se
exit 112
fi
The HTML is not displayed, the tabs are gone, but the newline is not replaced correctly:
Save the script echo to the file, and use Hexdump to see
Sure enough, the line break is not replaced. Why.
It turns out that by default SED will only handle rows , which means that sed discards line breaks and divides strings with newline characters into multiple lines, so that the swap line character is not replaced.
In this process, tried ${var/str2find/str2replace} syntax, also tried TR, neither. The first also does not handle newline characters; tr only replaces one to one, instead of replacing a newline character with multiple characters, which means you can replace "\ n" with "a", but you cannot replace "\ n" with "AAA". And back to Sed,google+man ...
Let's start by lighting up the final script and explaining it.
#! /bin/sh
result=$ (/bin/sh/etc/snmp/original.sh)
errorcount=$ (echo $result | egrep "Crazy stone| Xunzhen "| WC-L)
If [$errorCount-gt 0]; then
echo "$result" | sed ': label; N;s/\n/<br>/;t label ' | Sed ' s/\t//g '
exit/
Else
exit 112
fi
SED is powerful beyond imagination and is far more than just a string replacement. The above script uses the SED's label and n parameters to see the description in Man
: Label
Label for B and T commands.
T label
If a s///has done a successful substitution since the last input line is read and since the last T or T command, then B Ranch to label; The If label is omitted and branch to end of script.
n N read/append the next line of input into the pattern space.
A label acts like a goto in C, defines a label, and then executes it elsewhere where it can goto the label code, andn fills the next line into the current mode space , which contains the newline character, You can then replace the line characters.
The combination of label and n can be used to iterate over multiple lines of string, and the HTML display conforms to the expected