免费注册 查看新帖 |

Chinaunix

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

发现K shell下正则表达式的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-21 14:37 |只看该作者 |倒序浏览
例如用find命令(实际上,ls命令也一样)

# find /tmp/[A-Z]*

按理说应该是查找/tmp目录下的大写字母开头的文件或目录,但是,有趣的事情发生了:

(/bin/ksh下)
# find /tmp/[A-Z]*
/tmp/caadaptor.log
/tmp/cfl
/tmp/deviceMapping
/tmp/hsperfdata_noaccess
/tmp/hsperfdata_noaccess/2783
/tmp/hsperfdata_noaccess/5833
/tmp/hsperfdata_oamops
/tmp/hsperfdata_oamops/21061
/tmp/hsperfdata_oamops/29165
... ...

(/bin/sh下)
# find /tmp/[A-Z]*
/tmp/PPmgr
/tmp/PPmgr/mgrpid
/tmp/PPmgr/alrmthr3
/tmp/PPmgr/diskmonthr4
/tmp/PPmgr/convthr5
/tmp/PPmgr/convthr8
/tmp/PPmgr/dirmonthr10
/tmp/PPmgr/convthr6
/tmp/PPmgr/convthr7
/tmp/PPmgr/logthr9
/tmp/SunMCDBLogFiles
/tmp/SunMCDBLogFiles/db-start.log
/tmp/SunMCDBLogFiles/db-start-sql.log

/bin/ksh下命令把小写字母的目录和文件都列出来了,但是/bin/sh下结果正确。这是为什么呢?

现在我有一个ksh的脚本,我怎么能从当前的/bin/ksh环境切换到/bin/sh环境下执行find命令,执行完再切换会/bin/ksh?因为/bin/sh下的find执行的才正确。

例如:
# cat test.sh
#!/bin/ksh

find /tmp/[A-Z]* -print

....

我曾经试过这样的方法:
#!/bin/ksh

su - root -c "find /tmp/[A-Z]* -print"

....

如果root默认的SHELL是/bin/sh的话没问题,但是如果root默认SHELL是/bin/ksh的话一样存在问题。各位大虾有没有其他的解决方案?

论坛徽章:
0
2 [报告]
发表于 2010-04-21 14:44 |只看该作者
本帖最后由 wtuter 于 2010-04-21 14:48 编辑

回复 1# Bob.Wang


  /bin/sh  ufile_script

[2]echo $0
-bash
[2]

[2]ksh
# echo $0
ksh
# bash

论坛徽章:
0
3 [报告]
发表于 2010-04-21 14:48 |只看该作者
回复 2# wtuter


我用过

#!/bin/ksh

/bin/sh find /tmp/[A-Z]*


貌似不成啊,实际上我的一个ksh脚本里有个find命令执行有问题,我不能把我的整个shell从ksh转成sh,工作量太大了。

论坛徽章:
0
4 [报告]
发表于 2010-04-21 15:01 |只看该作者
回复 3# Bob.Wang


  放脚本里不行,因为你开头是#/bin/ksh

论坛徽章:
0
5 [报告]
发表于 2010-04-21 15:01 |只看该作者
好像找到一个方法了,不知道对不对:

#!/bin/ksh

/bin/sh -c "find /tmp/[A-Z]*"

论坛徽章:
0
6 [报告]
发表于 2010-04-21 15:26 |只看该作者
这个能算是ksh的一个bug么?

论坛徽章:
0
7 [报告]
发表于 2010-04-21 16:12 |只看该作者
标题很迷惑人啊。。明明8是正则

论坛徽章:
0
8 [报告]
发表于 2010-04-21 21:11 |只看该作者
你那个find用的就不对啊..

find /tmp -regex '/tmp/[A-Z].*'
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP