Shell string processing
String operation (length, read, replace)
ExpressionDescription$ {# String} $ string Length $ {string: position} in $ string, extract the substring from position $ {string: position: length} in $ string, extract the substring $ {string # substring} whose length is $ length from position $ position from the beginning of the variable $ string, delete the substring $ {string # substring} that matches the shortest $ substring from the beginning of the variable $ string, delete the substring $ {string % substring} that matches the longest $ substring from the end of the variable $ string, delete the substring $ {string % substring} that matches the shortest $ substring from the end of the variable $ string, delete the substrings that match the longest $ substring $ {string/substring/replacement} using $ replacement, to replace the first matched $ substring $ {string // substring/replacement} with $ replacement.AllMatched $ substring $ {string/# substring/replacement} if the $ stringPrefixIf $ substring is matched, $ replacement is used to replace the matched $ substring $ {string/% substring/replacement }.Suffix$ Substring is matched, and $ replacement is used to replace the matched $ substring.String operation example:
1. length $ string = linuxeye $ echo $ {# string} 82. truncation string $ string = linuxeye $ echo $ {string: 5} eye $ echo $ {string: 0: 5} # Starts from 0 by default and can be omitted, linux $ echo $ {string: 5} linux3. string to delete $ redis_file = c:/windows/src/redis-2.8.4.tar.gz $ echo $ {redis_file #/} c: /windows/src/redis-2.8.4.tar.gz $ echo $ {redis_file # */} windows/src/redis-2.8.4.tar.gz $ echo $ {redis_file # */unzip redis-2.8.4.tar.gz echo $ {redis_file %/*} c: /windows/src $ echo $ {redi S_file %/*} c: $ {variable name # substring Regular Expression} starts with a string and uses substring to delete matching expressions. $ {Variable name % substring Regular Expression} starts from the end of the string with substring to delete the matching expression. Note: $ {redis_file ### */} and $ {redis_file %/*} are the simplest methods to get the file name or directory address. 4. string replacement $ echo $ {redis_file/\// \\} c: \ windows/src/redis-2.8.4.tar.gz $ echo $ {redis_file //\// \\\} c: \ windows \ src \ redis-2.8.4.tar.gz $ {Variable/find/replace value} A "/" represents replacing the first, "//" represents replacing all, when the search appears: "/" Please add the Escape Character "\/" to indicate.
Judge to read string value
ExpressionDescriptionThe value of the variable var $ {var} is the same as that of $ var $ {var-DEFAULT}. If var is not declared, $ DEFAULT is used as its value $ {var: -DEFAULT} If var is not declared or its value is null, $ DEFAULT is used as its value, determine whether the var variable is defined $ {var = DEFAULT}. If var is not declared, use $ DEFAULT as its value $ {var: = DEFAULT}. If var is not declared, or if the value is null, use $ DEFAULT as its value to determine whether the var variable is not defined, and ensure that the variable always has a value $ {var + OTHER}. If var is declared, the value is $ OTHER. Otherwise, it is a null String $ {var: + OTHER}. If var is set, the value is $ OTHER, otherwise, it is a null String $ {var? ERR_MSG} If var is not declared, Print $ ERR_MSG $ {var :? ERR_MSG} If var is not set, Print $ ERR_MSG $ {! Varprefix *} matches all variables declared starting with varprefix $ {! Varprefix @} matches all variables declared starting with varprefix.Example of reading string values:
$ Output =$ {FILE:-UNSET} $ echo $ outputUNSET $ FILE =/root/lnmp $ output =$ {FILE: -UNSET} $ echo $ output/root/lnmp, prevent accidental deletion of variables: $ find $ {path-/tmp}-name * .tar.gz-type f | xargs rm-f