When doing a shell batch program, string related operations are often involved. There are a lot of command statements, such as: awk, sed can do a variety of string operations. In fact, the shell has a series of operation symbols built in, which can achieve similar effects. As you know, using internal operators will omit the time to start an external program, so the speed will be very fast.
First, judge the read string value
expression
meaning
${var} The value of the variable var, the same as $var ${var-DEFAULT} If var is not declared, then $DEFAULT is used as its value * ${var:-DEFAULT} If var is not declared, Or if the value is null, then $DEFAULT is used as its value * ${var=DEFAULT} If var is not declared, then $DEFAULT is used as its value * ${var:=DEFAULT} If var is not declared, or Its value is null, then $DEFAULT is its value * ${var+OTHER} If var is declared, its value is $OTHER, otherwise it is null string ${var:+OTHER} if var is set , then its value is $OTHER, otherwise it is a null string ${var?ERR_MSG} If var is not declared, then print $ERR_MSG * ${var:?ERR_MSG} If var is not set, then print $ERR_MSG * ${!varprefix*} matches all previously declared variables starting with varprefix ${!varprefix@} matches all previously declared variables starting with varprefix
Added“*” does not mean: Of course, if you change If var has been set, then its value is $var.
[chengmo@localhost ~]$ echo ${abc-'ok'}ok[chengmo@localhost ~]$ echo $abc
[chengmo@localhost ~]$ echo ${abc='ok'}ok[chengmo@localhost ~]$ echo $abcok
If abc does not declare “=" It will also assign a value to abc.
[chengmo@localhost ~]$ var1=11;var2=12;var3=[chengmo@localhost ~]$ echo ${!v@} var1 var2 var3[chengmo@localhost ~]$ echo ${ !v*}var1 var2 var3
${!varprefix*} Similar to ${!varprefix@}, you can search for variables already defined by variable name prefix characters, whether or not Null value.
Second, string operations (length, read, replace)
expression
meaning
${#string} $string length ${string:position} In $string, extract substring ${string:position:length} from position $position in $string, from The position $position starts to extract the substring length $$string#substring}. From the beginning of the variable $string, delete the substring with the shortest match $substring ${string##substring} from the beginning of the variable $string, delete the longest Substring matching $substring ${string%substring} From the end of the variable $string, delete the substring with the shortest match $substring ${string%%substring} From the end of the variable $string, delete the substring that matches the longest match $substring ${string/substring/replacement} uses $replacement instead of the first matching $substring ${string//substring/replacement} uses $replacement instead of all matching $substring ${string/#substring/replacement} The $string prefix matches $substring, then $replacement is used instead of the matched $substring ${string/%substring/replacement} if $ The string suffix matches $substring, then $replacement is used instead of the matched $substring
Description: "* $substring” can be a regular expression.
1. Length
[web97@salewell97 ~]$ test='I love china'[web97@salewell97 ~]$ echo ${#test}12
${#Variable name }Get string length
2. Intercept string
[chengmo@localhost ~]$ test='I love china'[chengmo@localhost ~]$ echo ${test:5} e china[chengmo@localhost ~]$ echo ${test:5:10} e china
${variable name: start: length} get substring
3.String deletion
[chengmo@localhost ~]$ test='c:/windows/boot.ini'[chengmo@localhost ~]$ echo ${test# /}c:/windows/boot.ini[chengmo@localhost ~]$ echo ${test#*/}windows/boot.ini[chengmo@localhost ~]$ echo ${test##*/}boot.ini < Br>
[chengmo@localhost ~]$ echo ${test%/*} c:/windows[chengmo@localhost ~]$ echo ${test%%/*}
${variable name #substring Regular Expression} Equipped with substring from the beginning of the string to remove the expression on the match.
${variable name %substring regular expression} is equipped with a substring from the end of the string to remove the expression on the match.
Note: ${test##*/}, ${test%/*} is the easiest way to get the file name, or directory address.
4.String replacement
[chengmo@localhost ~]$ test='c:/windows/boot.ini'[chengmo@localhost ~]$ echo ${test/\\//\\\\}c:\\windows/boot.ini[chengmo@localhost ~]$ echo ${test//\\//\\\\}c:\\windows\\boot.ini
${Variable/Find/Replace Value} One “/” means to replace the first one, ”//” means to replace all, when the search appears: & rdquo; /& rdquo; please add the escape character & rdquo; \\/” indicates.
Three, performance comparison
In the shell, through awk, sed, expr, etc. can be achieved, the string above operation. Below we perform a performance comparison.
[chengmo@localhost ~]$ test='c:/windows/boot.ini' [chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test} ;done;
real 0m0.173suser 0m0.139ssys 0m0.004s
[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $ Test);done;
real 0m9.734suser 0m1.628s
The speed difference is hundreds of times, calling external command processing, and the built-in operator performance is very different. In shell programming, try to do it with built-in operators or functions. Using awk, sed is similar to this.
First install CentOS6 to install rp-pppoe this software, the previous version of centos is installed
We all know how to query the installation time of win7 system? In fact, it is very simple, you can f
KVM: Kernel-based Virtual Machine is an open source virtualization solution based on the Linux kerne
Suddenly found that the website can not open, the database shows that the link can not be linked, th
Linux single network card configuration multiple IP
Linux loses Grub.conf file to start server
The difference between the two-layer, three-layer, and four-layer switches is
Linux partition knowledge and partition of large disk
Linux init detailed explanation
Liunx jdk installation tutorial
The problem of creating DBCA error problem under Linux operating system
Detailed linux and win partition format
Win7 shortcut key to send Win+E instantly turn on the computer
How to clean up the win7 right-click menu item
Win7, Win8.1 manually add right-click menu function skills
Win7 boot black screen error 15: file not found solution
Win8 desktop display network icon setting method
Win7/win8/win10 software can not be uninstalled how to do
Win7, Win8 system start black screen error 0xc000025 solution
The best solution for hard disk partitioning under Linux
Unable to update to Windows 8.1 solution
How to safely release memory in Win2003 (forcing the system to release all necessary resources)