免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345
最近访问板块 发新帖
楼主: ailms
打印 上一主题 下一主题

问 :关于 [a-z] 的问题 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2006-12-15 16:27 |只看该作者
原帖由 一梦如是 于 2006-12-15 14:00 发表
因为ksh没有采用这种机制 ^__^
我们看到的是bash的手册页,不是ksh的,不是么?


我倒!

论坛徽章:
0
42 [报告]
发表于 2006-12-15 16:53 |只看该作者
原帖由 woodie 于 2006-12-15 14:32 发表

梦兄这句容易产生误解,我再强调一下:准确地说是在
LC_COLLATE=C ls
这样的命令中才能这样讲。
可以肯定地说:环境变量LC_COLLATE的值是一定会影响字符排序的顺序的,进而也会对
[a-z]
这样统配符的扩展结 ...


明白了。

这时 LC_COLLATE “来得及” 影响后面的 bash ,所以导致 ll 输出的结果是我们想要的。


  1. [root@home root]# LC_COLLATE=C bash -c "ls -l /tmp/test/[a-z]*"  
  2. -rw-r--r--    1 root     root            0 12月 15 12:03 /tmp/test/a12
  3. [root@home root]#
复制代码


或许下面的例子更清楚


  1. [root@home test]# LC_COLLATE=C bash -c "echo $LC_COLLATE;ls -l [a-z]*"

  2. -rw-r--r--    1 root     root            0 12月 15 12:03 a12
  3. [root@home test]#
复制代码


因为 " " 中的 $LC_COLLATE 先被 shell 扩展了。所以实际上变成了 LC_COLLATE=C bash -c "echo ''; ls -l [a-z]*"

而下面的例子则不同


  1. [root@home test]# LC_COLLATE=C bash -c 'echo $LC_COLLATE;ls -l [a-z]*'     
  2. C
  3. -rw-r--r--    1 root     root            0 12月 15 12:03 a12
  4. [root@home test]#
复制代码


因为整个命令被 ' ' 括起来,所以 shell 不会先对 $LC_COLLATE 进行扩展,所以输出的值也就是 C 了

[ 本帖最后由 ailms 于 2006-12-15 17:21 编辑 ]

论坛徽章:
0
43 [报告]
发表于 2006-12-15 17:09 |只看该作者

顺便把 E9 部分贴上来,方便大家看

E9) Why does the pattern matching expression [A-Z]* match files beginning
    with every letter except `z'?

Bash-2.03, Bash-2.05 and later versions honor the current locale setting
when processing ranges within pattern matching bracket expressions ([A-Z]).
This is what POSIX.2 and SUSv3/XPG6 specify.

The behavior of the matcher in bash-2.05 and later versions depends on the
current LC_COLLATE setting.  Setting this variable to `C' or `POSIX' will
result in the traditional behavior ([A-Z] matches all uppercase ASCII
characters).  Many other locales, including the en_US locale (the default
on many US versions of Linux) collate the upper and lower case letters like
this:

        AaBb...Zz

which means that [A-Z] matches every letter except `z'.  Others collate like

        aAbBcC...zZ

which means that [A-Z] matches every letter except `a'.

The portable way to specify upper case letters is [:upper:] instead of
A-Z; lower case may be specified as [:lower:] instead of a-z.

Look at the manual pages for setlocale(3), strcoll(3), and, if it is
present, locale(1).  If you have locale(1), you can use it to find
your current locale information even if you do not have any of the
LC_ variables set.

My advice is to put

        export LC_COLLATE=C

into /etc/profile and inspect any shell scripts run from cron for
constructs like [A-Z].  This will prevent things like

        rm [A-Z]*

from removing every file in the current directory except those beginning
with `z' and still allow individual users to change the collation order.
Users may put the above command into their own profiles as well, of course.


不过有个问题,ls 似乎不识别 [:upper:] 和 [:lower:] ,但同样在 shell 下 tr 却可以,


  1. [root@home test]# ls
  2. 123  A  a12
复制代码


  1. [root@home test]# ls |tr -d '[:upper:]'
  2. 123

  3. a12
复制代码


  1. [root@home test]# ls [:upper:]
  2. ls: [:upper:]: 没有那个文件或目录

  3. [root@home test]# ls '[:upper:]'
  4. ls: [:upper:]: 没有那个文件或目录

  5. [root@home test]# ls "[:upper:]"
  6. ls: [:upper:]: 没有那个文件或目录
  7. [root@home test]#
复制代码


这是否又是 ls 命令自身的问题呢?

[ 本帖最后由 ailms 于 2006-12-15 17:11 编辑 ]

论坛徽章:
0
44 [报告]
发表于 2006-12-15 17:14 |只看该作者
Bash-2.03, Bash-2.05 and later versions honor the current locale setting
when processing ranges within pattern matching bracket expressions ([A-Z]).
This is what POSIX.2 and SUSv3/XPG6 specify


至于 bash -2.03 ,2.0.5 ~ 这样做有什么好处吗?似乎不大符合平常的习惯啊。

况且象 E9 说的,rm -f [A-Z] 就可以“不知不觉”中多删除了文件,岂不是更危险?

论坛徽章:
0
45 [报告]
发表于 2006-12-15 17:33 |只看该作者
发现应该用 ls [[:upper:]] ,而不是 ls [:upper:]


  1. [root@home test]# ls [[:upper:]]
  2. A
  3. [root@home test]#
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP