fant0men

arch_unpack.sh

Feb 25th, 2020 (edited)
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script extracts archives and then lets you know if there were
  3. # errors or not.
  4.  
  5. set -o pipefail
  6.  
  7. stderr_f="/dev/shm/arch_pack_stderr-${RANDOM}.txt"
  8. c_tty=$(tty)
  9.  
  10. # trap ctrl-c and call ctrl_c()
  11. trap ctrl_c INT
  12.  
  13. ctrl_c () {
  14.     restore
  15.     echo '** Trapped CTRL-C'
  16.     exit
  17. }
  18.  
  19. restore () {
  20.     exec 2>"$c_tty"
  21.     cat_stderr
  22.     rm -f "$stderr_f"
  23. }
  24.  
  25. cat_stderr () {
  26.     stderr_out=$(cat "$stderr_f")
  27.  
  28.     truncate -s 0 "$stderr_f"
  29.  
  30.     if [[ $stderr_out ]]; then
  31.         echo "$stderr_out"
  32.     fi
  33. }
  34.  
  35. touch "$stderr_f"
  36. exec 2>>"$stderr_f"
  37.  
  38. # Creates a function, called 'usage', which will echo usage instructions
  39. # and then exit.
  40. usage () {
  41.     cat <<USAGE
  42. Usage: $(basename "$0") [archives...]
  43.  
  44. Supported archive formats are:
  45.  
  46. dar, tar, tar.bz2|tbz|tbz2|bz2, tar.xz|txz|xz, tar.gz|tgz|gz, zip, 7z, rar, cab|exe, Z, arj, iso
  47.  
  48. USAGE
  49.  
  50.     exit
  51. }
  52.  
  53. # Creates a function, called 'check_cmd', which will be used to
  54. # check if the needed commands are installed.
  55. check_cmd () {
  56.     check () {
  57.         command -v "$1"
  58.     }
  59.  
  60.     declare -A cmd name
  61.  
  62.     cmd[dar]=$(check 'dar')
  63.     cmd[7z]=$(check '7za')
  64.     cmd[rar]=$(check 'rar')
  65.     cmd[cab]=$(check 'cabextract')
  66.     cmd[arj]=$(check '7z')
  67.  
  68.     name[dar]='dar'
  69.     name[7z]='7zip'
  70.     name[rar]='rar'
  71.     name[cab]='cabextract'
  72.     name[arj]='7zip'
  73.  
  74.     for cmd_type in ${!cmd[@]}; do
  75.         if [[ $1 == $cmd_type ]]; then
  76.             if [[ -z ${cmd[${cmd_type}]} ]]; then
  77.                 cat <<CMD
  78.  
  79. '${name[${cmd_type}]}' is not installed!
  80. Install it through your package manager.
  81.  
  82. In the case of 'rar', you can get the Linux version for free @ https://www.rarlab.com/download.htm
  83. Put the 'rar' executable in ${HOME}/bin, and make sure that this directory is in your PATH variable.
  84. You can modify your PATH variable by editing ${HOME}/.bashrc, and adding this line:
  85.  
  86. PATH="\${HOME}/bin:\${PATH}"
  87.  
  88. CMD
  89.                 exit
  90.             fi
  91.         fi
  92.     done
  93. }
  94.  
  95. # If there are no arguments to the script, print usage and then exit.
  96. if [[ ! -f $1 ]]; then
  97.     usage
  98. fi
  99.  
  100. # Creates a function, called 'output', which will let the user know if
  101. # the extraction went okay or not. If not, print the entire output from
  102. # the compression program.
  103. output () {
  104.     print_stdout () {
  105.         for (( n = 0; n < ${last}; n++ )); do
  106.             echo "${stdout_v[${n}]}"
  107.         done
  108.         unset -v stdout_v
  109.         cat_stderr
  110.     }
  111.    
  112.     if [[ ${#stdout_v[@]} -gt 0 ]]; then
  113.         last=$(( ${#stdout_v[@]} - 1 ))
  114.     fi
  115.  
  116.     if [[ ${stdout_v[${last}]} -ne 0 ]]; then
  117.         echo -e "${f}: Something went wrong\n"
  118.         print_stdout
  119.     else
  120.         echo -e "${f}: Everything is Ok\n"
  121. #       print_stdout
  122.     fi
  123. }
  124.  
  125. # Creates a function, called 'iso_extract', which will be used to mount,
  126. # copy files from, and unmount an ISO file. This in effect means
  127. # extracting the ISO.
  128. iso_extract () {
  129.     iso_bn="${f_bn%.???}"
  130.     iso_mnt="/dev/shm/${f_bn}-${RANDOM}"
  131.     iso_of="${PWD}/${iso_bn}-${RANDOM}"
  132.  
  133.     echo -e "${iso_of}: Creating output directory...\n"
  134.     mkdir "$iso_mnt" "$iso_of"
  135.  
  136.     echo -e "${f}: Mounting...\n"
  137.     sudo mount "$f" "$iso_mnt" -o loop
  138.  
  139.     echo -e "${f}: Extracting files...\n"
  140.     cp -p -r "$iso_mnt"/* "$iso_of"
  141.  
  142.     echo -e "${iso_of}: Changing owner to ${USER}...\n"
  143.     sudo chown -R "${USER}:${USER}" "$iso_of"
  144.     sudo chmod -R +rw "$iso_of"
  145.  
  146.     echo -e "${f}: Unmounting...\n"
  147.     sudo umount "$iso_mnt"
  148.  
  149.     echo -e "${iso_mnt}: Removing mountpoint...\n"
  150.     rm -rf "$iso_mnt"
  151. }
  152.  
  153. while [[ $# -gt 0 ]]; do
  154.     f=$(readlink -f "$1")
  155.     f_bn=$(basename "$f")
  156.     f_bn_lc=$(tr '[[:upper:]]' '[[:lower:]]' <<<"$f_bn")
  157.  
  158.     if [[ ! -f $f || ! -r $f ]]; then
  159.         usage
  160.     fi
  161.  
  162.     case "$f_bn_lc" in
  163.         *.dar)
  164.             check_cmd dar
  165.  
  166.             dar_of="${PWD}/${f_bn}"
  167.             dar_of=$(sed -E 's/(\.[0-9]+){0,}\.dar//' <<<"$dar_of")
  168.             dar_of="${dar_of}-${RANDOM}"
  169.             mkdir "$dar_of"
  170.  
  171.             dar -x "$f" -R "$dar_of"
  172.             cat_stderr
  173.         ;;
  174.         *.tar)
  175.             mapfile -t stdout_v < <(tar -xf "$f"; echo "$?")
  176.             output
  177.         ;;
  178.         *.tar.bz2|*.tbz|*.tbz2)
  179.             mapfile -t stdout_v < <(tar -xjf "$f"; echo "$?")
  180.             output
  181.         ;;
  182.         *.tar.xz|*.txz)
  183.             mapfile -t stdout_v < <(tar -xJf "$f"; echo "$?")
  184.             output
  185.         ;;
  186.         *.tar.gz|*.tgz)
  187.             mapfile -t stdout_v < <(tar -xzf "$f"; echo "$?")
  188.             output
  189.         ;;
  190.         *.gz)
  191.             mapfile -t stdout_v < <(gunzip "$f"; echo "$?")
  192.             output
  193.         ;;
  194.         *.bz2)
  195.             mapfile -t stdout_v < <(bunzip2 "$f"; echo "$?")
  196.             output
  197.         ;;
  198.         *.xz)
  199.             mapfile -t stdout_v < <(unxz "$f"; echo "$?")
  200.             output
  201.         ;;
  202.         *.zip)
  203.             mapfile -t stdout_v < <(unzip "$f"; echo "$?")
  204.             output
  205.         ;;
  206.         *.7z)
  207.             check_cmd 7z
  208.  
  209.             mapfile -t stdout_v < <(7za x "$f"; echo "$?")
  210.             output
  211.         ;;
  212.         *.rar)
  213.             check_cmd rar
  214.  
  215.             mapfile -t stdout_v < <(rar x "$f"; echo "$?")
  216.             output
  217.         ;;
  218.         *.cab|*.exe)
  219.             check_cmd cab
  220.  
  221.             mapfile -t stdout_v < <(cabextract "$f"; echo "$?")
  222.             output
  223.         ;;
  224.         *.z)
  225.             mapfile -t stdout_v < <(uncompress "$f"; echo "$?")
  226.             output
  227.         ;;
  228.         *.arj)
  229.             check_cmd arj
  230.  
  231.             mapfile -t stdout_v < <(7z x "$f"; echo "$?")
  232.             output
  233.         ;;
  234.         *.iso)
  235.             iso_extract
  236.         ;;
  237.     esac
  238.  
  239.     shift
  240. done
  241.  
  242. restore
  243.  
RAW Paste Data