将状态(文件完整性)检查添加到cbr cbz转换bash脚本

第一篇文章,嗨!首先让我说我是一个关于编程的总菜鸟.我理解非常基本的东西,但是当谈到检查退出代码或适当的术语时,我很茫然.显然我的搜索结果在这个领域真的很弱,我想这是一个术语问题.

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

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

问题:有时重新包装文件并不能很好地结束,并且希望通过完整性检查来减轻这些问题.另一个去.我稍微修改了这个脚本以使用p7zip,因为它可以打包/解压缩7z,zip文件和some others,i.非常适合选择. p7zip可以通过以下方式测试存档:

7z t comicfile.cbz tmpworkingdir

我想这是使用if&的问题.否则在这里(?)检查完整性,然后再给它一次,如果有任何错误.

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

#!/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的废话.. [编辑]啊…… mh-mmm ..我们去..

[编辑2]我删除了unrar作为依赖项并使用p7zip,因为它可以提取rar文件.

最佳答案 您需要两张支票:

> 7z t将测试存档的完整性
>您还应该测试存档中所有图像文件的完整性.您可以使用像ImageMagick这样的工具.

一个简单的测试是识别文件,但可能只读取标题.我使用转换文件-resize 5×5 png: – >的/ dev / null的

这会将图像缩小到5×5像素,将其转换为PNG,然后将结果传递给/ dev / null(丢弃它).对于缩放,必须读取整个图像.如果此命令失败并显示错误,则图像文件出现问题.

点赞