Version 0.1 of the script for automatic par2 repair and unpacking

I’ve decided to call it “par2unpack.sh”, a contraction of “par2verify” and “newsunpack”. I’m not going to post the whole script here, just the two methods for normal directory unpacking and recursive unpacking.
If anyone wants to try it in a shell script, be my guest, but you do so at your own risk! If it deletes every single file in your home directory, or causes your smelly socks to knock out small children and Republicans under the age of five, then that is *your* problem ;)

All you need to do is open a text editor, put #!/bin/bash at the top and copy paste the code below. And you obviously also need newsunpack.py in the directory /usr/local/bin for it to work. Then save the file and make it executable. That would be a quick way to try it, I’m still working on the exception handling side (e.g. what to do when the wrong number/type of arguments are provided). And I’m still learning, so here goes nothing:


# If one argument is passed, and it is a directory, then unpack 
# using the .par2/.PAR2 files. TODO: currently does not support 
# automatic unpacking if you *only* have parity archives with 
# .volXX+YY.par2 extensions. Use manual unpacking instead.
if [ $# -eq 1 ] && [ -d $1 ]
then
ls $1/*{.par2,.PAR2} 2> /dev/null | while read \
parfile;
do
# Grep matches any file ending in .volX+Y.par2 and #.volX+Y.PAR2, 
# where X and Y are a series of numbers, thus effectively eliminating
# them from the loop. 
if [[ `echo $parfile | grep '.*.vol[0-9]\+[+-][0-9]\+ \
.[pP][aA][rR]2'` ]] 
then
continue
fi
    
if [[ -f $parfile ]]
then
/usr/local/bin/newsunpack.py -d "$parfile"
fi
done
exit 0
fi

# If the -r option is used, the script recursively unpacks in sub-
# directories too, using find. But as before, it skips the volumes 
# with recovery slices (the files with ".volXX-YY"  in their 
# extensions).
if [ $# -eq 2 ] && [ $1 == "-r" ] && [ -d $2 ]
then
for dir in `find $2 -type d` ;
do	
ls "$dir"/*par2 2> /dev/null | while read parfile;
do
if [[ `echo $parfile | grep '.*.vol[0-9]\+[+-][0-9]\+ \
.[pP][aA][rR]2'` ]]
then
continue
fi				
if [[ -f $parfile ]]
then
/usr/local/bin/newsunpack.py -d "$parfile"
fi
done
done
exit 0
fi

[EDIT]
It seems I just cannot post code on this blog or something. Using <pre> tags makes it possible for code to extend all the way off the text area, and onto the links and everything, on the right. Which, needless to say, is extremely annoying. I’ll have to do some searching on the web to find a solution for this major inconvenience.
[/EDIT]
[EDIT2]
I’m now using backslashes (“\”) to make bash continue to next line. For now it’s the best way, it seems, I can fit the code on the page, but the code should still work.
[/EDIT2]

Advertisements

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: