【发布时间】:2013-11-27 09:37:41
【问题描述】:
谁能帮我解决这个问题?
我正在尝试将图像从我的 USB 复制到我计算机上的存档中,我决定制作一个 BASH 脚本来简化这项工作。我想复制文件(即 IMG_0101.JPG),如果存档中已经有一个具有该名称的文件(我每次使用它时都会擦拭相机),该文件应该命名为 IMG_0101.JPG.JPG 所以我不会丢失文件。
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: $0 image_path archive_path"
exit 999
fi
if [ -d "$1" ] #Checks if archive directory exists
then
echo Image Source directory FOUND
else
echo ERROR: Image Source directory has NOT BEEN FOUND
fi
if [ -d "$2" ]
then
echo Photo Archive FOUND
else
echo Creating directory
mkdir "$2"
fi
if [ find $1 -name "IMG_[0-9][0-9][0-9][0-9].JPG" ] #added this in to be more specific 1/4
then #2/4
for file in "$1"/*
do
dupefile= "$2"/"$file"
while [ -e "$newfile" ];
do
newfile=$newfile.JPG
done
mv "$file" "$newfile"
done
else #3/4
#do nothing
fi #4/4 took all the /4 out, but it's saying theres no such file or directory, even though I've tested it and it says there is.
unexpected token fi 是我得到的错误,但 if 语句需要在那里,所以我需要的特定文件正在移动。
【问题讨论】:
-
为什么不简单地在每个文件前加上索引号?或者你甚至可以用这个数字替换整个名称,因为文件名对你来说似乎并不重要。
-
您应该确保您从不使用
mv覆盖任何现有文件,即使是由于脚本中的错误。为此,只需将选项-n添加到mv。 -
你好像把
newfile和dupefile搞混了。坚持一个或另一个。 -
您应该在
exit分支中使用非零结果代码ERROR。但是,您不能使用大于 255 的结果代码。
标签: linux bash shell scripting