土问,关于置顶中的shell基础问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
kobe082002
帖子: 53
注册时间: 2005-12-01 1:21
送出感谢: 0
接收感谢: 0

土问,关于置顶中的shell基础问题

#1

帖子 kobe082002 » 2006-08-11 9:57

里面关于here documents这一段不是太明白,我把程序复制后在ubuntu里面运行,发生错误:
cat: renames: No such file or directory
cat: a: No such file or directory
cat: number: No such file or directory
cat: of: No such file or directory
cat: files: No such file or directory
cat: using: No such file or directory
cat: sed: No such file or directory
cat: regular: No such file or directory
cat: expressions: No such file or directory
./ren: line 6: USAGE:: command not found
./ren: line 8: EXAMPLE:: command not found
./ren: line 9:  ren: command not found
./ren: line 11: HELP: command not found
./ren: line 12:  exit: command not found
./ren: line 22: syntax error near unexpected token `then'
./ren: line 22: `  if [ -f "$file" ] ; then'

我刚开始学shell,哪位大虾能帮忙解答一下,thx! :)
头像
kobe082002
帖子: 53
注册时间: 2005-12-01 1:21
送出感谢: 0
接收感谢: 0

#2

帖子 kobe082002 » 2006-08-13 9:18

没人回答,只好自问自答了
原来那个脚本估计是楼主贴的时候出错了,漏了一下代码,完整的代码如下:
#!/bin/sh
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ] ; then
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
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
回复

回到 “Shell脚本”