博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[bash] string operators
阅读量:5092 次
发布时间:2019-06-13

本文共 4533 字,大约阅读时间需要 15 分钟。

4.3.4. Extended Pattern Matching

Bash provides a further set of pattern matching operators if the shopt option extglob is switched on. Each operator takes one or more patterns, normally strings, separated by the vertical bar ( | ). The extended pattern matching operators are given in .

[8] Be aware that these are not available in early releases of bash 2.0.

Table 4-3. Pattern-matching operators

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.

Table 4-2. Pattern-matching operators

Operator

Meaning

${

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 beginning of the variable's value, delete the longest part that 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 pattern matches the end of the variable's value, delete the longest part that matches and return the rest.

${

variable/pattern/string}${
variable//pattern/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.

Table 4-1. Substitution operators

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} sets 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 shells 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 the existence of a variable.

Example: ${count:+1} returns 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 starts 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 the 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} returns footman. ${count:4:4} returns foot.

Some examples of these include:

  • *(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, or hare.

  • !(alice|hatter|hare) matches everything except alice, hatter, and hare.

The values provided can contain shell wildcards too. So, for example, +([0-9]) matches a number of one or more digits. The patterns can also be nested, so you could remove all files except those beginning with vt followed by a number by doing rm !(vt+([0-9])).

省略

Searching...

转载于:https://www.cnblogs.com/friedwm/archive/2011/08/10/2133323.html

你可能感兴趣的文章
OO设计的接口分隔原则
查看>>
数据库连接字符串大全 (转载)
查看>>
java类加载和对象初始化
查看>>
对于负载均衡的理解
查看>>
django简介
查看>>
window.event在IE和Firefox的异同
查看>>
常见的js算法面试题收集,es6实现
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Windows10 下Apache服务器搭建
查看>>
HDU 5458 Stability
查看>>
左手坐标系和右手坐标系
查看>>
solr后台操作Documents之增删改查
查看>>
http://yusi123.com/
查看>>
文件文本的操作
查看>>
Ubuntu linux下gcc版本切换
查看>>
记一次Web服务的性能调优
查看>>
jQuery.form.js使用
查看>>
(转)linux sort,uniq,cut,wc命令详解
查看>>
关于ExecuteNonQuery执行的返回值(SQL语句、存储过程)
查看>>
UVa540 Team Queue(队列queue)
查看>>