Objective
Now every time you analyze the site log, you need to judge Baidu Spider is not the real spider, nslookup after the need to determine whether the results include "Baidu" string
Here are some of the methods that are included in the judgment string in the shell, source programmer question and answer website StackOverflow and Segmentfault.
Method one: using grep to find
Stra= "Long string"
strb= "string"
result=$ (echo $strA | grep "${STRB}")
if [["$result"!= ""]]
Then
echo "contains"
else
Echo does not contain "
fi"
Print a long string, then grep in a long string to find the string to search for and record results with variable result
If the result is not empty, the stra contains STRB. If the result is empty, the description is not included.
This method leverages the grep's characteristics and is the most concise.
Method two: Using string operators
Stra= "HelloWorld"
strb= "Low"
if [[$strA =~ $strB]]
then
echo contains "
else
echo" does not contain "
fi
Use string operator =~ to directly determine whether Stra contains STRB. (This is not more concise than the first method!) )
Method Three: Using wildcard characters
A= "HelloWorld"
b= "Low"
if [[$A = = * $B *]]
then
echo "contains"
else
echo "does not contain"
fi
This is also easy, with the wildcard * Number agent stra parts of STRB, if the result is equal to the description contained, otherwise not included.
Method Four: Using the case in statement
Thisstring= "1 2 3 4 5" # source string
searchstring= "1 2" # search string case
$thisString in
* "$searchString" *) echo Enemy Spot;;
*) echo nope;;
Esa
This is more complicated, the case in I have not yet contacted, but since there is a relatively simple way why so
Method V: Using replacement
String_a=$1
string_b=$2
if [[${string_a/${string_b}//} = = $STRING _a]]
then
# # is not substring.< C23/>echo N return
0
Else
# # is substring.
echo Y return
1
fi
This is complicated.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.