作用:判断文件类型,根据判断结果使用不同的命令完成解压操作。
经典:${1##*.} 表示对第一个参数按##后面的模式从左边匹配,返回剩余的未匹配部分,且是最大匹配
${1%.*}表示对第一个参数按%后面的模式从右边匹配,返回剩余的未匹配部分,且是最小匹配
如:.1.2.3.4.5.6.7.8 按${1##.*} 匹配则匹配全部,剩余空字符串,按${1%.*}匹配则匹配.8,剩余.1.2.3.4.5.6.7
这两个用法是自己琢磨出来的,若有不对欢迎补充!
#!/bin/bash
UNPACK=1
if [ ${1##*.} = bz2 ] ; then
TEMP=${1%.*}
if [ ${TEMP##*.} = tar ] ; then
tar jxvf $1
UNPACK=$?
echo This is a tar.bz2 package
else
bunzip2 $1
UNPACK=$?
echo This is a bz2 package
fi
fi
if [ ${1##*.} = gz ] ; then
TEMP=${1%.*}
if [ ${TEMP##*.} = tar ] ; then
tar zxvf $1
UNPACK=$?
echo This is a tar.gz package
else
gunzip $1
UNPACK=$?
echo This is a gz package
fi
fi
if [ ${1##*.} = tar ] ; then
tar xvf $1
UNPACK=$?
echo This is a tar package
fi
if [ $UNPACK = 0 ] ; then
echo Success!
else
echo Maybe it is not a package or the package is damaged?
fi