Truncate string
$ Echo 'expr substr 123 2 1'
Truncate from 0 to 3
$ Echo $ {path: 0: 3}
Replace spaces-
$ Echo "a B" | TR """-"
Var1 = "hello"
Var2 = "he"
String inclusion
Method 1-> intercept a string
Echo $ {var1: 0: 2} # extract 2 bits starting from 0->$ {Variable: 0: n}
If [$ {var1: 0: 2 }=$ var2]; then
Echo "1: Include"
Fi
Method 2
-Eq-> two equal values (equal)
$? -> Check the execution result of the last command. The value 0 indicates normal, and the value 0 indicates abnormal. [The default value is 1].
Grep "keywords" File
-> If keywords are available, the output result code is 0.
-> NO keywords does not output content, but the result code is 1 [Echo $?]
-> Grep "keywords" file-Q [-Q-> quiet; Do not write anything to standard output. exit immediately with zero status if any match is found, even if an error was detected.]
Echo "$ var1" | grep-Q "$ var2"
If [$? -EQ 0]; then
Echo "2: Include"
Fi
# Method 3
Echo "$ var1" | grep-Q "$ var2" & Echo "include" | echo "not"
# Method 4
[["$ {Var1/$ var2 /}"! = "$ Var2"] & Echo "include" | echo "not"
This article is from the "andyhdchoice" blog, please be sure to keep this source http://andyhdchoice.blog.51cto.com/1521623/1548777
Shellscript: String operations