Search This Blog

Friday, July 28, 2006

解压一大堆文件的脚本

#!/bin/bash
do_dir ()
{
cd $1
for i in *
do
if [[ -f $i ]]
then
decompress $i
elif [[ $i == "." ]] || [[ $i == ".." ]]
then
continue
elif [[ -d $i ]]
then
[[ $recue == 1 ]] && do_dir $i
fi
done
}


decompress ()
{
case $1 in

*.tar.bz2)
tar jxvf $1
;;
*.tar.gz)
tar zxvf $1
;;
*.rar)
rar x $1
;;
*.gz)
gzip -d $1
;;
*.bz2)
bzip2 -d $1
;;
*)
echo "$1:not a compressed file,do nothing"
return
;;
esac
[[ $del == 1 ]] && rm $1
}


get_help ()
{
echo "this bash script is written to get all the compressed files under a directory to be uncompressed."
echo "Usage:tarball.sh [OPTION] [DIR]"
echo "OPTION:"
echo -e "\t-r\trecursively"
echo -e "\t-d\tremove the file after decompress it"
}



if [[ $# == 0 ]]
then
echo "no arguments detected,do you want to continue or just get help?[c/H]:"
read help
fi


[[ $help != "c" ]] && get_help && exit 0

for arg in $@
do
case $arg in
-r)
recue=1
;;
-d)
del=1
;;
-h)
get_help
exit 0
;;
-*)
echo "unknown arg:$arg"
echo "type tarball.sh -h to get help"
get_help
exit 1
;;
*)
[[ $arg != $0 ]] && [ -d $arg ] && name=$arg
;;
esac
done



if [[ -n $name ]]
then
do_dir $name
else
do_dir "."
fi

No comments: