【问题标题】:Md5 Hash to identify and archive images用于识别和存档图像的 Md5 哈希
【发布时间】: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检查常见错误。

标签: linux bash shell hash md5


【解决方案1】:

你没有注意文件夹和文件名中的空格。

例如:

cp $line $2 

应该是:

cp "$line" "$2"

您应该首先通过评估您引用的每个变量并添加“”来消除这些空格作为错误的来源。

如果您仍然收到错误,请向我们提供使用的参数以及不存在的目录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 2015-09-23
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多