Õâ¸ö½Å±¾¼ÙÉèÄãµÄrarÎĵµÀïÃæ¸ùĿ¼ֻÓÐÒ»¸ö£¬Á½¸öµÄ»° ¶ÔÓÚ´íÎóĿ¼µÄɾ³ý»áֻɾµÚÒ»¸öĿ¼¡£½é¸öÀÁµÃ¸ÄÁË ºÇºÇ
x@XEROO:~$ cat rarx.sh
#!/bin/bash
#
# Author: JayXie
# Date: 2006-03-24
# Email: oxwjo@163.com
#
E_BADARGS=65
E_NOFILE=66
E_NODIR=67
help()
{
echo -e "Usage: `basename $0`
}
if [ $# -eq 0 ]
then
help
exit $E_BADARGS
fi
while [ -n "$1" ]; do
case $1 in
-h) help;exit 0;;
-*) echo -e "error: no such option $1. -h for help\n";exit $E_BADARGS;;
*) break;;
esac
done
if [ ! -f "$1" ]
then
echo -e "File \"$1\" does not exist.\n"
exit $E_NOFILE
fi
# Get file list from archive
rar vt $1 | tac | sed -e '1,/--------/d' > /tmp/rarx.$$
# Make directorys
current_dir=`pwd`
if [ -n "$2" ]
then
if [ ! -d "$2" ]
then
echo -e "Directory \"$2\" dose not exist.\n"
exit $E_NODIR
fi
cd "$2"
fi
{
while read line
do
case $line in
*.D....*) read line;echo "$line" >> /tmp/rarx.Adir.$$;mkdir -p "$line";;
------*) break;;
*);;
esac
done
}
cd "$current_dir"
# Unrar archive
rar x $1 $2
# Remove wrong directorys
read first_dir < /tmp/rarx.Adir.$$
if [ -n "$2" ]
then
cd $2
fi
ls -A -c1 > /tmp/rarx.Sdir.$$
ls $first_dir -ARF -c1 | sed -n /:/p | sed s/://g >> /tmp/rarx.Sdir.$$
{
while read line
do
if [ -d "$line" ]
then
grep -q "$line" /tmp/rarx.Adir.$$
if [ $? -eq 0 ]
then
grep -q "$line$" /tmp/rarx.Adir.$$
if [ $? -eq 1 ]
then
rmdir "$line"
fi
fi
fi
done
}
rm /tmp/rarx.$$
rm /tmp/rarx.Adir.$$
rm /tmp/rarx.Sdir.$$

