根据不同的文件结尾后缀对文件进行解压缩

extract_file.sh

#!/bin/bash
#Author:ztj
#Date:2023/5/6
#Description:extract compressed file

NOARGS=65
NOTFOUND=66
Out_Directory=/tmp

if [ $# -eq 0 ];then
  echo "Usage: `basename $0` filename" >&2
  exit $NOARGS
fi

filename=$1

if [ ! -f "$filename" ];then
  echo "File $filename not found,please check it!" >&2
  exit $NOTFOUND
else
  case $filename in
  *.gz) 
     tar -zxvf $filename -C $Out_Directory &>/dev/null
     echo "$filename is extracted successfully."
     ;;
  *.bz2) 
     tar -jxvf $filename -C $Out_Directory &>/dev/null
     echo "$filename is extracted successfully."
     ;;
  *.xz)
     tar -Jxvf $filename -C $Out_Directory &>/dev/null
     echo "$filename is extracted successfully."
     ;;
  *.tar)
     tar -xvf $filename -C $Out_Directory &>/dev/null
     echo "$filename is extracted successfully."
     ;;
  *) 
     echo "对不起,无法识别文件格式!"
     ;;
  esac
fi