Monday, August 12, 2013

bash 笔记(二)——字符串操作


一个常用的字符串操作是放在变量名的{}内的一些特定操作符,见下表。

Operator Substitution
${varname:-word} If varname exists and isn't null, return its value; otherwise return word.
Purpose: Returning a default value if the variable is undefined.
Example: ${count:-0} evaluates to 0 if count is undefined.
${varname:=word} If varname exists and isn't null, return its value; otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way.
Purpose: Setting a variable to a default value if it is undefined.
Example: ${count:=0} set count to 0 if it is undefined.
${varname:?message} If varname exists and isn't null, return its value; otherwise print varname:followed by message, and abort the current command or script(non-interactive shell only). Omitting message produces the default message parameter null or not set.
Purpose: Catching errors that result from variables being undefined.
Example: {count:?”undefined!”} prints “count: undefined!” and exits if count is undefined.
${varname:+word} If varname exists and isn't null, return word; otherwise return null.
Purpose: Testing for existence of a variable.
Example: ${count:+1} return 1 (which could mean “true”) if count is defined.
${varname:offset:length} Performs substring expansion. It returns the substring of $varname starting at offset and up to length characters. The first character in $varname is position 0. if length is omitted, the substring start at offset and continues to the end of $varname. If offset is less than 0 then the position is taken from the end of $varname. If varname is @, the length is number of positional parameters starting at parameter offset.
Purpose: Returning parts of a string(substrings or slices).
Example: if count is set to frogfootman, ${count:4} return footman. ${count:4:4} return foot.
表中除了最后一项,其余各项中的冒号(:)是可以省略的。如果没有冒号,则测试条件由“exists and isn't null" 变为 “exists",即仅仅测试变量是否存在。

模式匹配(Patterns and Pattern Matching)


Operator Substitution
${variable#pattern} If the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest.
${variable##pattern} If the pattern matches the begginning of the variable's value, delete the longest part thar matches and return the rest.
${variable%pattern} If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest.
${variable%%pattern} If the parttern matches the end of the variable's value, delete the longest part the matches and return the rest.
${variable/parttern/string}
${variable//parttern/string}
The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.
记忆小巧门:#是用在数字开头的,所以对应变量开始部分。%总是用在数字的后面,所以对应变量的结尾部分。

此类模式匹配比较经典的应用是对路径名的解析比如假设:

path=/home/david/document/long.file.name 

则:


${path##/*/}
                     long.file.name
${path#/*/}
      david/document/long.file.name
$path
/home/david/document/long.file.name
${path%.*}
/home/david/document/long.file
${path%%.*}
/home/david/document/long

最长和最短匹配只有在有通配符*存在的情况下才生效,否则他们的输出结果是一样的。比如:filename=alicece ,则${filename%ce} 和 ${filename%%ce} 两个输出的结果都是 alice .

长度操作符

 ${#varname}


*扩展的模式匹配

如果用shopt把extglob设置打开,则可以一次提供多个模式匹配,之间用|分隔。

Operator Meaning
*(patternlist) Matches zero or more occurrences of the given patterns.
+(patternlist) Matches one or more occurrences of the given patterns.
?(patternlist) Matches zero or one occurrences of the given patterns.
@(patternlist) Matches exactly one of the given patterns.
!(patternlist) Matches anything except one of the given patterns.
例子:

  • *(alice|hatter|hare) would match zero or more occurrences of alice, hatter, and hare. So it would match the null string, alice, alicehatter, etc.
  • +(alice|hatter|hare) would do the same except not match the null string.
  • ?(alice|hatter|hare) would only match the null string, alice, hatter, or hare.
  • @(alice|hatter|hare) would only match alice, hatter, and hare.
  • !(alice|hatter|hare) matches everything except alice, hatter and hare.

模式字串里可以包含shell的通配符,比如:+([0-9])可以匹配一位或多位的数字。模式还可以嵌套,比如 rm !(vt+([0-9])) 可以删除所有文件除了文件名由vt开始后跟一串数字的文件。

No comments:

Search This Blog