Adding status check (file integrity) to cbr cbz bash script conversion

First post, so hello! Let me start with what I'm talking about programming. I understand very simple stuff, but when it comes to checking exit codes or what is a suitable term, I'm at a loss. Apparently, my search engine is really weak in this area, I think this is a matter of terminology.

Thank you in advance for reading this / answering my question!

Description : I found a script that converts / repackages .cbr files to .cbz. These files are basically your average rar and zip files, however renamed to another extension, as they are used for (comic) book applications like comicrack , qcomicbook , and what not. Surprisingly, there are no cbr β†’ cbz converters. The benefits of .cbz also avoid the proprietary rar file format that you can store metadata from Comic Vine with e. g comictagger .

Problem . Sometimes repackaging files does not end well and, hopefully, will be mitigated by integrity checking and another. I changed the script a bit to use p7zip , since it can like pack / unpack 7z , zip files and some others , i.e. e great for options. p7zip can test the archive:

7z t comicfile.cbz tmpworkingdir 

I assume it is a matter of using if and else here (?) To check the integrity, and then give it one more step if there are any errors.

Question / tl; dr: What would be the β€œbest” / adequate approach for adding file integrity checking to the script below?

 #!/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 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 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" 

Oh mankind, without publishing more than two links up to 10 reputation, and I linked the shit from OP .. [edit] ah .. mh-mmm .. there we go ..

[edit 2] I removed unrar as a dependency and used p7zip instead, since it can extract rar files.

+4
source share
1 answer

You will need two checks:

  • 7z t will check archive integrity
  • You should also check the integrity of all image files in the archive. You can use tools like ImageMagick to do this .

    A simple test would be an identify file, but it can only read the header. I would use the convert file -resize 5x5 png:- > /dev/null

    This scales the image to 5x5 pixels, converts it to PNG, and then transfers the result to /dev/null (discarding it). To scale, you need to read the entire image. If this command fails, something is wrong with the image file.

+2
source

All Articles