Guest User

Untitled

a guest
Jun 19th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/bin/bash
  2. #----------------------------------------------------
  3. # Version: 0.2.4.0
  4. # Author: Florian "Bluewind" Pritz <f-p@gmx.at>
  5. #
  6. # Licensed under WTFPL v2
  7. # (see COPYING for full license text)
  8. #---------------------------------------------------
  9. # Extract an archive based on the mime type
  10. #----------------------------------------------------
  11.  
  12. SCRIPTNAME=$(basename $0)
  13. ARGS=""
  14.  
  15. EXIT_SUCCESS=0
  16. EXIT_FAILURE=1
  17. EXIT_ERROR=2
  18. EXIT_BUG=10
  19.  
  20. # Colors for output
  21. red='\e[0;31m'
  22. RED='\e[1;31m'
  23. green='\e[0;32m'
  24. GREEN='\e[1;32m'
  25. blue='\e[0;34m'
  26. BLUE='\e[1;34m'
  27. cyan='\e[0;36m'
  28. CYAN='\e[1;36m'
  29. NC='\e[0m'
  30.  
  31. function usage {
  32. echo -e "${blue}Usage:${NC} ${SCRIPTNAME} [OPTIONS] <files>" >&2
  33. echo -e "Options:" >&2
  34. echo -e "-C <dir> Extract content of the archive(s) to <dir> (not yet fully supported for all archives)" >&2
  35. echo -e "-R Remove archive after extracting" >&2
  36. echo -e "-v Verbose" >&2
  37. [[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
  38. }
  39.  
  40. function clean {
  41. if [ "$REMOVE_AFTER" = "true" ]; then
  42. rm $file
  43. fi
  44. }
  45.  
  46. while getopts ':C:vhR' OPTION ; do
  47. case $OPTION in
  48. v)
  49. ARGS=${ARGS}" -v "
  50. UNZIPARGS=$UNZIPARGS""
  51. ;;
  52. C)
  53. TARARGS=${ARGS}" -C $OPTARG"
  54. UNZIPARGS=$UNZIPARGS" -d $OPTARG"
  55. ;;
  56. h)
  57. usage $EXIT_SUCCESS
  58. ;;
  59. R)
  60. REMOVE_AFTER=true
  61. ;;
  62. \?)
  63. echo "Unknown option \"-$OPTARG\"." >&2
  64. usage $EXIT_ERROR
  65. ;;
  66. :)
  67. echo "Option \"-$OPTARG\" needs an argument" >&2
  68. usage $EXIT_ERROR
  69. ;;
  70. *)
  71. echo "This shouldn't happen, please file a bugreport.">&2
  72. usage $EXIT_BUG
  73. ;;
  74. esac
  75. done
  76.  
  77.  
  78. shift $(( OPTIND - 1 ))
  79.  
  80. if (( $# < 1 )) ; then
  81. usage $EXIT_SUCCESS
  82. fi
  83.  
  84. for file; do
  85. if [ -f "${file}" ] ; then
  86. case "${file}" in
  87. *.tar.bz2) tar ${ARGS} ${TARARGS} -xjf "${file}" && clean;;
  88. *.tar.gz) tar ${ARGS} ${TARARGS} -xzf "${file}" && clean;;
  89. *.tar.xz) tar ${ARGS} ${TARARGS} -xf "${file}" && clean;;
  90. *.tbz2) tar ${ARGS} ${TARARGS} -xjf "${file}" && clean;;
  91. *.tgz) tar ${ARGS} ${TARARGS} -xzf "${file}" && clean;;
  92. *.7z) 7z x ${file} && clean;;
  93. *)
  94. case $(file -b --mime-type "${file}") in
  95. application/x-bzip2) bunzip2 "${file}" && clean;;
  96. application/x-xz) xz -d "${file}" && clean;;
  97. application/x-rar) unrar x "${file}" && clean;;
  98. application/x-gzip) gunzip "${file}" && clean;;
  99. application/x-tar) tar ${ARGS} ${TARARGS} -xf "${file}" && clean;;
  100. application/zip) unzip ${UNZIPARGS} "${file}" && clean;;
  101. application/x-compressed) uncompress "${file}" && clean;;
  102. application/x-7z-compressed) 7z x "${file}" && clean;;
  103. *) echo -e "${RED}Error:$NC No rule how to extract \"${file}\" ($(file -b --mime-type "${file}"))";;
  104. esac
  105. ;;
  106. esac
  107. else
  108. echo -e "${RED}Error:$NC \"${file}\" doesn't exist"
  109. fi
  110. done
Add Comment
Please, Sign In to add comment