Bourne Shell脚本完成工作但产生额外的消息

时间:2013-05-21 01:03:12

标签: shell sh

因此,脚本应该采用文件扩展名和可能的多个文件来更改其扩展名。它适用于大多数,但是当文件中有空格时,它会更改它,然后说该文件不存在。继承人我拥有的......

#!/bin/sh
fileExtension="$1"
shift
oldName="$@"
extension=${oldName##*.}
totalFiles=$#
totalFiles=$(( totalFiles+1 ))

num=1
while [ $num -lt $totalFiles ]
do
   for i in "$oldName"
   do
      extension=${i##*.}
      if test -e "$i" then
          newName="${i%.*}.$fileExtension"
          if [ "$i" = "$newName" ]
          then
             :
          else
              mv "$i" "$newName"
          fi
      else
          echo "$i": No such file
      fi
      num=$(( num+1 ))
      shift
      done
done

3 个答案:

答案 0 :(得分:0)

你不能迭代一个字符串,至少不是你的方式。 oldName需要是一个数组

# other stuff
oldName=("$@")
# other stuff
for i in "${oldName[@]}"
# other stuff

答案 1 :(得分:0)

为什么不简化它:

#!/bin/sh
fileExtension="$1"
shift
for file in "$@"
do
    extension=${file##*.}
    if [ -e "$file" ]
    then
        newName="${file%.*}.$fileExtension"
        if [ "$file" != "$newName" ]
        then mv "$file" "$newName"
        fi
    else
        echo "$file: No such file" >&2
    fi
done

答案 2 :(得分:0)

我得到了这个......

#!/bin/sh
fileExtension="$1"
shift
for file in "$@"
do
    if test -e "$file"
    then
    newName="${file%.*}.$fileExtension"
        if test "$file" = "$newName"
        then
        :
        else
            mv "$file" "${file%.*}.$fileExtension"
    fi
    else
        echo "$file": No such file >&2
    fi
done