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