• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

批量将大写文件名转换成小写

发布: 2007-6-08 22:43 | 作者: seanhe | 来源: | 查看: 108次 | 进入软件测试论坛讨论

领测软件测试网
master于7.29 17:54整理
 


首先介绍一下tr命令的一个用法tr string1 string2, 
string1/2指定同等数量的字符集合,输入字符在string1中 
寻找,若找到,则输出时替换成string2中相应位置的字符。 
#! /bin/csh 
if ( $#argv != 1  then 
    echo " Usage: $0 -l|-u " 
    exit 1 
endif 
if ( "$1" != "-l" && "$1" != "-u"  then 
    echo " Usage: $0 -l|-u " 
    exit 1 
endif 
if ( "$1" == "-l"  then 
    foreach file ( *  
        mv $file `echo $file | tr '[A-Z]' '[a-z]'` 
    end 
else 
    foreach file ( *  
        mv $file `echo $file | tr '[a-z]' '[A-Z]'` 
    end 
endif 
  
对于ksh,介绍一下typeset -l varname命令, 
这个命令的意思是$varname中的所有字符 
将被转换成小写。相应的是-u参数。 
#! /bin/ksh 
if [ $# -ne 1 ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ $1 != "-l" -a $1 != "-u" ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
typeset $1 targetFile 
for file in * 
do 
    targetFile="$file" 
    mv $file $targetFile 
done 
  
再介绍一下expr string1 : string2命令,string1是待处理 
字符串,string2是一个正则表达式,输出将是匹配处理后的结果。 
之所以要多费点手脚,因为可能文件名目录名存在特殊字符, 
比如空格等,expr string1 : string2这个命令不管文件名目 
录名里有没有特殊字符都会输出原始名称。 
#! /bin/sh 
if [ $# -ne 1 ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ $1 != "-l" -a $1 != "-u" ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ "$1" = "-l" ] 
then 
    for file in * 
    do 
        targetFile=`expr "+++$file" : '+++\(.*\)' | tr '[A-Z]' '[a-z]'` 
        mv "$file" "$targetFile" 
    done 
else 
    for file in * 
    do 
        targetFile=`expr "+++$file" : '+++\(.*\)' | tr '[a-z]' '[A-Z]'` 
        mv "$file" "$targetFile" 
    done 
fi 
这里所有的示例都假设只处理了当前子目录下的本层的文件, 
如要进行目录树的处理,可以配合使用find命令。上面的 
+++仅仅是为了便于匹配处理排除一些干扰因素,没有别的 
实际意义。

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网