【问题标题】:Adding a status (file integrity)check to a cbr cbz converting bash script向 cbr cbz 转换 bash 脚本添加状态(文件完整性)检查
【发布时间】:2023-04-03 23:20:01
【问题描述】:

第一次发帖,嗨!首先让我说我是一个关于编程的菜鸟。我了解非常基本的东西,但是在检查退出代码或适当的术语是什么时,我不知所措。显然我的 searchfoo 在这方面确实很弱,我想这是一个术语问题。

提前感谢您抽出宝贵时间阅读本文/回答我的问题!

说明:我发现了一个script,它可以将 .cbr 文件转换/重新打包为 .cbz 文件。这些文件基本上是您的平均rar 和zip 文件,但是重命名为另一个扩展名,因为它们用于(漫画)书应用程序,例如comicrack、qcomicbook 等等。令人惊讶的是,那里没有 cbr -> cbz 转换器。 .cbz 的优点除了转义 专有 rar 文件格式外,还可以使用 e 存储来自Comic Vine 的元数据。 gcomictagger.

问题:有时文件的重新打包不会很好地结束,并且希望通过完整性检查和另一次尝试来缓解。我稍微修改了所述脚本以使用p7zip,因为它可以打包/解包7z、zip-files 和some others,即。 e 非常适合选择。 p7zip 可以通过以下方式测试存档:

7z t comicfile.cbz tmpworkingdir

我想这是一个使用 if & else here(?) 来检查完整性的问题,如果有任何错误,请再试一次。

问题/tl;dr:向下面的脚本添加完整性文件检查的“最佳”/适当方法是什么?

#!/bin/bash
#Source: http://comicrack.cyolito.com/forum/13-scripts/30013-cbr3cbz-rar-to-zip-conversion-for-linux
echo "Converting CBRs to CBZs"

# Set the "field separator" to something other than spaces/newlines" so that spaces
# in the file names don't mess things up. I'm using the pipe symbol ("|") as it is very
# unlikely to appear in a file name.
IFS="|"

# Set working directory where to create the temp dir. The user you are using must have permission
# to write into this directory.
# For performance reasons I'm using ram disk (/dev/shm/) in Ubuntu server.
WORKDIR="/dev/shm/"

# Set name for the temp dir. This directory will be created under WORDDIR
TEMPDIR="cbr2cbz"

# The script should be invoked as "cbr2cbz {directory}", where "{directory}" is the
# top-level directory to be searched. Just to be paranoid, if no directory is specified,
# then default to the current working directory ("."). Let's put the name of the
# directory into a shell variable called SOURCEDIR.
# Note: "$1" = "The first command line argument"
if test -z "$1"; then
        SOURCEDIR=`pwd`
else
        SOURCEDIR="$1"
fi

echo "Working from directory $SOURCEDIR"

# We need an empty directory to work in, so we'll create a temp directory here
cd "$WORKDIR"
mkdir "$TEMPDIR"
# and step into it
cd "$TEMPDIR"

# Now, execute a loop, based on a "find" command in the specified directory. The
# "-printf "$p|" will cause the file names to be separated by the pipe symbol, rather than
# the default newline. Note the backtics ("`") (the key above the tab key on US
# keyboards).
for CBRFILE in `find "$SOURCEDIR" -name "*.cbr" -printf "%p|while read line; do

    # Now for the actual work. First, extract the base file name (without the extension)
    # using the "basename" command. Warning: more backtics.
    BASENAME=`basename $CBRFILE ".cbr"`

    # And the directory path for that file, so we know where to put the finished ".cbz"
    # file.
    DIRNAME=`dirname $CBRFILE`

    # Now, build the "new" file name,
    NEWNAME="$BASENAME.cbz"

    # We use RAR file's name to create folder for unpacked files
    echo "Processing $CBRFILE"
    mkdir "$BASENAME"
    # and unpack the rar file into it
    7z x "$CBRFILE" -O"$BASENAME"
    cd "$BASENAME"

    # Lets ensure the permissions allow us to pack everything
    sudo chmod 777 -R ./*

# Put all the extracted files into new ".cbz" file
    7z a -tzip -mx=9 "$NEWNAME" *

    # And move it to the directory where we found the original ".cbr" file
    mv "$NEWNAME" $DIRNAME/"$NEWNAME"

    # Finally, "cd" back to the original working directory, and delete the temp directory
    # created earlier.
    cd ..
    rm -r "$BASENAME"

    # Delete the RAR file also
    rm "$CBRFILE"

done

# At the end we cleanup by removing the temp folder from ram disk
cd ..
echo "Conversion Done"
rm -r "$TEMPDIR"

哦,人性啊,在 10 名声望之前不要发布超过两个链接,我将 OP 中的废话链接了。 /p>

[edit 2] 我将 unrar 作为依赖项删除,并改用 p7zip,因为它可以提取 rar 文件。

【问题讨论】:

  • +1 让您摆脱了链接限制(我希望您保存旧版本以便以后更新您的帖子)。
  • 首先,您应该引用$SOURCEDIR(在 find 命令中),因为它可能包含空格。最好引用$TEMPDIR 和$WORKDIR(尽管您自己设置它们)。删除WORKDIR = "/dev/shm/" 中= 前后的空格。它应该WORKDIR="/dev/shm/"。还要在test -z $1 中引用$1。也不解析查找。改用while循环,如find ..... | while read line; do
  • @user000001 谢谢!尽管在您的建议看到 OP 中的脚本后,我似乎得到了以下错误,(注意这很可能是我的菜鸟的结果)。 /usr/local/bin/cbr2cbzTEST:第 49 行:在寻找匹配的 ``' 时出现意外的 EOF/usr/local/bin/cbr2cbzTEST:第 84 行:语法错误:文件意外结束
  • @danielpublic 你误解了我的评论。 for 不再需要。整行应该是find "$SOURCEDIR" -name "*.cbr" | while read CBRFILE; do。有了这个也不需要设置IFS。它将处理文件名中的所有字符(换行符 '\n' 除外 - 无论如何都不应该使用)

标签: bash zip 7zip rar


【解决方案1】:

您需要进行两项检查:

  1. 7z t 将测试存档的完整性
  2. 您还应该测试存档中所有图像文件的完整性。为此,您可以使用ImageMagick 等工具。

    一个简单的测试是identify file 但它可能只读取标题。我会使用convert file -resize 5x5 png:- > /dev/null

    这会将图像缩小到 5x5 像素,将其转换为 PNG,然后将结果通过管道传输到 /dev/null(丢弃它)。对于缩放,必须读取整个图像。如果此命令失败并出现错误,则图像文件有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2014-11-12
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多