分页: 1 / 1

对置顶贴中的"shell基础"有点不明白

发表于 : 2008-03-22 12:49
yanglh
#!/bin/sh
# 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

HELP
 exit 0
fi
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of
# command line arguments.
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
上面这段程序中的
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

HELP
 exit 0
fi
对这部分有些疑问. "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

HELP

发表于 : 2008-03-22 12:59
BigSnake.NET
是有误, 应该是

代码: 全选

cat <<HELP
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

HELP 

发表于 : 2008-03-22 18:02
yanglh
OK
谢谢BigSnake.NET