免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1073 | 回复: 0
打印 上一主题 下一主题

String operators in bash shell [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-09 12:25 |只看该作者 |倒序浏览

  Why string operators?

  • Ensure that variables exist (i.e., are defined and have non-null values)

  • Set default values for variables

  • Catch errors that result from variables not being set

  • Remove portions of variables' values that match patterns
    Table 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.
    [5]
    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.
    Table 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.
    [6]


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22500/showart_153065.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP