Advertisement
Guest User

xtract

a guest
Jan 22nd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.37 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. logloc=$HOME/logs
  4.  
  5. info () {
  6.     echo "INFO:"
  7.     echo "Current log file location is set to $logloc"
  8.     echo "All files extrated will have full paths"
  9. }
  10.  
  11. timestamp () {
  12.     date +"- - - - - - - - %F %R - - - - - - - - "
  13. }
  14.  
  15. tarcheck () {
  16.     command -v tar > /dev/null 2>&1
  17.     if [[ $? -eq 0 ]]; then
  18.         echo "tar is installed - proceeding"
  19.         return 0
  20.     else
  21.         echo "tar is not installed, please install it with your preferred package manager"
  22.         return 1
  23.     fi
  24. }
  25.  
  26. xtract () {
  27.     case "$@" in
  28.         *.zip )
  29.             echo "File format is zip - using unzip"
  30.             echo "Checking if unzip is installed"
  31.             command -v unzip > /dev/null 2>&1
  32.             if [[ $? -eq 0 ]]; then
  33.                 echo "unzip is installed - proceeding"
  34.  
  35.                 timestamp >> $logloc/$@.log
  36.                 echo "Checking archive integrity" | tee -a $logloc/$@.log
  37.                 unzip -t $@  >> $logloc/$@.log 2>&1
  38.                 if [[ $? -eq 0 ]]; then
  39.                     echo "Tests went ok. Proceeding with extraction..." | tee -a $logloc/$@.log
  40.                     echo "~~~~~~~~~~~~~~~~~ EXTRACTING ~~~~~~~~~~~~~~~~~" | tee -a $logloc/$@.log
  41.                     unzip $@ 2>&1 >> $logloc/$@.log
  42.                     if [[ $? -eq 0 ]]; then
  43.                         echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  44.                         exit 0
  45.                     else
  46.                         echo "Errors occured during extraction. Check log file for details. Aborting..."
  47.                         exit 1; fi
  48.                 else
  49.                     echo "File is corrupt. Aborting..."
  50.                     exit 1; fi
  51.             else
  52.                 echo "unzip is not installed, please install it with your preferred package manager"
  53.                 exit 1; fi
  54.             ;;
  55.  
  56.         *.rar ) # UNFINISHED
  57.                 # TODO: format properly
  58.                 #       maybe some spinner?
  59.             echo "File format is rar - using unrar"
  60.             echo "Checking if unrar is installed"
  61.             command -v unrar > /dev/null 2>&1
  62.             if [[ $? -eq 0 ]]; then
  63.                 echo "unrar is installed - proceeding"
  64.                 echo "Checking archive integrity"
  65.                 unrar t $@  >> $logloc/$@.log 2>&1
  66.                 if [[ $? -eq 0 ]]; then
  67.                     echo "Tested ok. Proceeding to extract..."
  68.                     unrar x $@ 2>&1 >> $logloc/$@.log
  69.                     if [[ $? -eq 0 ]]; then
  70.                         echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  71.                         exit 0
  72.                     else
  73.                         echo "Errors occured during extraction. Check log file for details. Aborting..."
  74.                         exit 1; fi
  75.                 else
  76.                     echo "File is corrupt. Aborting..."
  77.                     exit 1; fi
  78.             else
  79.                 echo "unrar is not installed, please install it with your preferred package manager"
  80.                 exit 1; fi
  81.             ;;
  82.  
  83.         *.tar ) # UNFINISHED
  84.             echo "File format is tar - using tar"
  85.             echo "Checking if tar is installed"
  86.             tarcheck
  87.  
  88.             timestamp >> $logloc/$@.log
  89.             tar -xvf $@
  90.             ;;
  91.  
  92.         *.tar.gz )
  93.             echo "File format is tar.gz - using tar"
  94.             echo "Checking if tar is installed"
  95.             tarcheck
  96.             if [[ $? -eq 0 ]]; then
  97.                 timestamp >> $logloc/$@.log
  98.                 tar -xzvf $@ 2>&1 >> $logloc/$@.log
  99.                 if [[ $? -eq 0 ]]; then
  100.                     echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  101.                     exit 0; fi
  102.                 else
  103.                     echo "Errors occured. Aborting..."
  104.                     exit 1; fi
  105.             ;;
  106.  
  107.         *.tar.xz )
  108.             echo "File format is tar.xz - using tar"
  109.             echo "Checking if tar is installed"
  110.             tarcheck
  111.             if [[ $? -eq 0 ]]; then
  112.                 timestamp >> $logloc/$@.log
  113.                 tar -xJvf $@ 2>&1 >> $logloc/$@.log
  114.                 if [[ $? -eq 0 ]]; then
  115.                     echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  116.                     exit 0; fi
  117.                 else
  118.                     echo "Errors occured. Aborting..."
  119.                     exit 1; fi
  120.             ;;
  121.  
  122.         *.tar.bz2 )
  123.             echo "File format is tar.gz - using tar"
  124.             echo "Checking if tar is installed"
  125.             tarcheck
  126.             if [[ $? -eq 0 ]]; then
  127.                 timestamp >> $logloc/$@.log
  128.                 tar -xjvf $@ 2>&1 >> $logloc/$@.log
  129.                 if [[ $? -eq 0 ]]; then
  130.                     echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  131.                     exit 0; fi
  132.                 else
  133.                     echo "Errors occured. Aborting..."
  134.                     exit 1; fi
  135.             ;;
  136.  
  137.         *.7z )
  138.             echo "File format is 7z - using 7z"
  139.             echo "Checking if 7z is installed"
  140.             command -v 7z > /dev/null 2>&1
  141.             if [[ $? -eq 0 ]]; then
  142.                 echo "7z is installed - proceeding"
  143.  
  144.                 timestamp >> $logloc/$@.log
  145.                 echo "Checking archive integrity" | tee -a $logloc/$@.log
  146.                 7z t $@  >> $logloc/$@.log 2>&1
  147.                 if [[ $? -eq 0 ]]; then
  148.                     echo "Tests went ok. Proceeding with extraction..." | tee -a $logloc/$@.log
  149.                     echo "~~~~~~~~~~~~~~~~~ EXTRACTING ~~~~~~~~~~~~~~~~~" | tee -a $logloc/$@.log
  150.                     7z x $@ 2>&1 >> $logloc/$@.log
  151.                     if [[ $? -eq 0 ]]; then
  152.                         echo "Files from $@ extracted successfuly" | tee -a $logloc/$@.log
  153.                         exit 0
  154.                     else
  155.                         echo "Errors occured during extraction. Check log file for details. Aborting..."
  156.                         exit 1; fi
  157.                 else
  158.                     echo "File is corrupt. Aborting..."
  159.                     exit 1; fi
  160.             else
  161.                 echo "7z is not installed, please install it with your preferred package manager"
  162.                 exit 1; fi
  163.             ;;
  164.  
  165.         * )
  166.             echo "Unknown file format"
  167.             exit 1
  168.             ;;
  169.     esac
  170. }
  171.  
  172. info
  173. xtract $@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement