- #!/bin/sh
- #先判断参数 参数要3个以上
- # we have less than 3 arguments. Print the help text:
- if [ $# -lt 3 ] ; then
- cat <
- ren -- renames a number of files using sed regular expressions
- USAGE: ren 'regexp' 'replacement' files...
- EXAMPLE: rename all *.HTM files in *.html:
- #这里使用ren 'HTM$' 'html' *.HTM ...'HTM$' 这是指文件名的尾部,作者提示这样可以漂亮修改后缀名。
- ren 'HTM$' 'html' *.HTM
- HELP
- exit 0
- fi
- #这里是取前面两个知,就是替换旧文件名部分字符串 和 新的字符串
- OLD="$1"
- NEW="$2"
- # The shift command removes one argument from the list of
- # command line arguments.
- #这里比较关键,两次shift就是把$3变成$1,下面才能正常使用$*,才可以正常取文件列表
- shift
- shift
- # $* contains now all the files:
- #这里就是处理过程了
- for file in $*; do
- if [ -f "$file" ] ; then
- #这里没多大意义,就是输出处理了什么
- newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
- if [ -f "$newfile" ]; then
- echo "ERROR: $newfile exists already"
- else
- echo "renaming $file to $newfile ..."
- mv "$file" "$newfile"
- fi
- fi
- done