Version 0.2, a new version of par2unpack.sh

I’ve now started rewriting the original script, since the original was unable to deal with cases where only vol.XX+YY.PAR2 files are available to repair a set of files. For now, this version only repairs all files in one directory. It only unpacks a file, if the filename without suffix has *changed*. This way, it avoids unpacking the same file over and over again. For example, suppose a directory contains the following files:
file1.vol01+00.PAR2
file1.vol01+08.PAR2
file1.vol02+13.PAR2
file2.par2
file2.vol01+77.PAR2

Without any kind of string comparing, newsunpack would unpack file1 three times, and file2 two times! The script I am writing would unpack file1.vol01+00.PAR2, and then skip all the other names starting with file1, and then unpack file2.par2.
Here is what I got so far:


#!/bin/bash

unpack() {
oldfile=$1
ls *[pP][aA][rR]2 2> /dev/null | while read parfile ; do
if [ `echo "$parfile" | grep '..vol[0-9]\+[+-][0-9]\+.[pP][aA][rR]2'` ]
then
newfile=${parfile%.vol*[-+]*.[pP][aA][rR]2}
else
newfile=${parfile%.[pP][aA][rR]2}
fi

if [ $newfile == $oldfile ]
then
continue
else
/usr/local/bin/newsunpack.py $parfile
oldfile=$newfile
fi
done
return 0
}

if [ $# -eq 1 ] && [ -d $1 ]
then
pushd "$1" > /dev/null 2>&1
ls *[pP][aA][rR]2 2> /dev/null | while read parfile ; do
if [[ `echo "$parfile" | grep '..vol[0-9]\+[+-][0-9]\+.[pP][aA][rR]2'` ]]
then
firstfile=${parfile%.vol*[-+]*.[pP][aA][rR]2}
else
firstfile=${parfile%.[pP][aA][rR]2}
fi
/usr/local/bin/newsunpack.py $parfile
unpack $firstfile
popd > /dev/null 2>&1
break
done
exit 0
fi

Notice the break statement near the end. I’m afraid that is because I have not been able to find a way to read just the first par2 file in a directory. So I am using a while loop, and then simply breaking out of it at the end so it does not do any looping at all. It’s not pretty, but it works!

I apologize if it looks like gobbledygook, I am not using those <pre> tags anymore, as I have found that using them makes the code continue all the way off the page, and end up somewhere in China. And I just don’t know of any better way to post code examples in my posts, sorry :(

Advertisements

One Response to Version 0.2, a new version of par2unpack.sh

  1. […] code from my blog I’ve just discovered that using copy/past to grab a copy of the par2unpack.sh script I posted WILL RENDER THE SCRIPT NON-EXECUTABLE! : par2unpack.sh […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: