gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:14:49 26 4
gpt4 key购买 nike

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

预先感谢您花时间阅读本文/回答我的问题!

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

问题:有时文件的重新打包不会很好地结束,希望通过完整性检查和另一次尝试来缓解。我稍微修改了上述脚本以使用 p7zip因为它可以打包/解包 7z , zip 文件和 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 的废话.. [edit]啊.. mh-mmm.. 我们开始了..

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

最佳答案

您需要进行两项检查:

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

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

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

关于bash - 将状态(文件完整性)检查添加到 cbr cbz 转换 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15110518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com