免费注册 查看新帖 |

Chinaunix

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

[其他] bash ${^^} [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-02-01 16:37 |只看该作者 |倒序浏览
本帖最后由 hbmhalley 于 2013-02-01 16:40 编辑

  1. $ b=abcd

  2. $ echo ${b^a} # O
  3. Abcd

  4. $ echo ${b^ab} # X
  5. abcd

  6. $ echo ${b^d} # X
  7. abcd

  8. $ echo ${b^^d} # O
  9. abcD

  10. $ echo ${b^^cd} # X
  11. abcd

  12. $ echo ${b%d} # O
  13. abc

  14. $ bash --version
  15. GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
复制代码
为什么三个标 X 的没有成功替换成大写?

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
2 [报告]
发表于 2013-02-01 16:59 |只看该作者
这么高级的功能没用过呢,看来我是没机会用了……
  1. [seesea@UC ~]$ b=abcd
  2. [seesea@UC ~]$ echo ${b^a}
  3. -bash: ${b^a}: bad substitution
  4. [seesea@UC ~]$ echo ${b^^a}
  5. -bash: ${b^^a}: bad substitution
  6. [seesea@UC ~]$ bash --version
  7. GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
  8. Copyright (C) 2005 Free Software Foundation, Inc.

复制代码
  1. seesea@UC ~/桌面
  2. $ b=abcde

  3. seesea@UC ~/桌面
  4. $ echo ${b^a}
  5. bash: ${b^a}: bad substitution

  6. seesea@UC ~/桌面
  7. $ bash --version
  8. GNU bash, version 2.05b.0(1)-release (i686-pc-cygwin)
  9. Copyright (C) 2002 Free Software Foundation, Inc.
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-02-01 17:19 |只看该作者
  1. 站个楼 下个bash 4看看去
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
4 [报告]
发表于 2013-02-01 17:48 |只看该作者
bash 4 新功能吧,还没用过。

论坛徽章:
0
5 [报告]
发表于 2013-02-01 20:43 |只看该作者
@seesea2517
@V_客
@blackold

大家平时都在什么环境下?如果有包管理工具的话应该不会长时间不更新吧

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2013-02-01 20:46 |只看该作者
       ${parameter^pattern}
       ${parameter^^pattern}
       ${parameter,pattern}
       ${parameter,,pattern}
              Case modification.  This expansion modifies the case of alphabetic characters in parameter.  The pattern is expanded to produce  a  pattern  just  as  in  pathname
              expansion
.   The  ^ operator converts lowercase letters matching pattern to uppercase; the , operator converts matching uppercase letters to lowercase.  The ^^ and
              ,, expansions convert each matched character in the expanded value; the ^ and , expansions match and convert only the first character in the expanded  value..   If
              pattern  is  omitted, it is treated like a ?, which matches every character.  If parameter is @ or *, the case modification operation is applied to each positional
              parameter in turn, and the expansion is the resultant list.  If parameter is an array variable subscripted with @ or *, the case modification operation is  applied
              to each member of the array in turn, and the expansion is the resultant list.

首先要搞清楚什么是pathname expansion,这一点很关键。
其次要分清^和^^,前者是first,后者是each
你先仔细想想,实在不明白再问。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
7 [报告]
发表于 2013-02-01 20:56 |只看该作者
回复 5# hbmhalley


    我只是玩,工作很少用到。
   
    之前还及时更新,后来觉得没有必要,就改为必要时才更新——不更新不行了,或者有问题了。

论坛徽章:
0
8 [报告]
发表于 2013-02-01 21:01 |只看该作者
回复 6# Shell_HAT


    其实我还真不是太清楚,一直当成弱版正则
    但:
       ${parameter%word}
       ${parameter%%word}
              Remove matching suffix pattern.  The word is expanded to produce  a  pattern
              just as in pathname expansion.  If the pattern matches a trailing portion of
              the expanded value of parameter, then the result of  the  expansion  is  the
              expanded  value  of  parameter with the shortest matching pattern (the ``%''
              case) or the longest matching pattern (the ``%%'' case) deleted.  If parame‐
              ter  is  @ or *, the pattern removal operation is applied to each positional
              parameter in turn, and the expansion is the resultant list.  If parameter is
              an  array variable subscripted with @ or *, the pattern removal operation is
              applied to each member of the array in turn, and the expansion is the resul‐
              tant list.

    这也是上面把 ${%}作为参照的原因
    而且 ${b^^abcd} 同样不成功

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
9 [报告]
发表于 2013-02-01 21:29 |只看该作者
回复 8# hbmhalley


    大小写转换时的pattern只能是一个character,不像子串删除或者替换的时候pattern可以是一个string

论坛徽章:
0
10 [报告]
发表于 2013-02-01 21:49 |只看该作者
本帖最后由 hbmhalley 于 2013-02-01 21:50 编辑

回复 9# Shell_HAT


    这样啊!
    但为什么手册里没发现 .. 难道我E文捉急了?
    &为什么要这么规定? 尤其是 ^^ 好像更适合匹配并替换字符串吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP