【发布时间】:2013-12-13 19:07:39
【问题描述】:
这是我的第一个 bash 脚本,我正在尝试消除所有折痕并让脚本运行良好。该脚本用于归档它在 HDD/闪存驱动器的多个目录中找到的所有指定 .jpg 文件。有些文件名称相同但内容不同,所以我使用了 Md5 总和对它们进行哈希处理。
我在 Geany 中收到目录不存在错误,但它从命令栏中运行良好,缺少两个图像。我已经尝试了我能想到的一切来修复它。是不是代码乱七八糟?
#!/bin/sh
if [ ! -d "$1" ]; then
echo Directory "$1" cannot be found. Please try again.
exit
fi
if [ $# -eq 1 ]; then
echo "usage: Phar image_path archive_path"
exit
fi
if [ -d "$2" ]; then
echo "archive exists"
else
echo "the directory 'archive' does't exist. Creating directory 'archive'."
mkdir -p ~/archive
fi
find $1 -iname "IMG_[0-9][0-9][0-9][0-9].JPG" | cat > list.txt
[ -f ~/my-documents/md5.txt ] && rm md5.txt || break
while read line;
do md5sum $line | xargs >> md5.txt
done < list.txt
sort -k 1,1 -u md5.txt | cat > uniquemd5.txt
cut -d " " -f 2- uniquemd5.txt > uniquelist.txt
sort uniquelist.txt -r -o uniquelist.txt
for line in $(cat uniquelist.txt)
do
file=$(basename $line) path="$2/file"
if [ ! -f $path ];
then
cp $line $2
else
cp $line $path.JPG
fi
done
【问题讨论】:
-
你是说它在交互式 shell 中工作,但从 Geany 调用时却不行?您是否检查过 Geany 使用哪个工作目录和参数调用它? (而且,如果你可以使用
fdupes)。 -
你可能打算在第 33 行写
path="$2/$file"。 -
您也可以使用shellcheck.net检查常见错误。