fant0men

arch_list.sh

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