#!/bin/bash # Error handling set -e # To be implemented: # dependencies=("echo" "sed" "tar" "zip" "bzip2" "gzip" "gunzip" "pax" "7z" "ar" "cpio" "brotli" "lzip" "rzip" "xz" "zstd") #Use for loop to check if recognized file formats have been passed to first argument # For styling/colorizing output txtbld=$(tput bold) txtblue=${txtbld}$(tput setaf 4) txtgreen=${txtbld}$(tput setaf 2) txtred=${txtbld}$(tput setaf 1) txtyellow=${txtbld}$(tput setaf 3) txtwhite=${txtbld}$(tput setaf 7) # Dependency check function dependencycheck() { numdependencies="$#" dependencies=("$@") missingdependencies=0 for ((i = 0; i < numdependencies; i++)) ; do if ! command -v "${dependencies[$i]}" &> /dev/null ; then echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}" missingdependencies=$((missingdependencies+1)) fi done if [ $missingdependencies -gt 0 ] ; then echo "${txtred}Please install needed dependencies${txtwhite}" exit 1 fi } # Main Dependency check dependencycheck echo sed # Intro Prompt echo "${txtgreen}xtract: a simple shell script for decompressing common file formats" # Reset output style tput bold & tput setaf 7 # grabs the extension string extension=$(echo "${1}" | sed 's/^[^\..:]*[\..]//') # create an array and a for loop of various # ".txt" and ".js" and ".c" and ".py" files etc... for here # if the file is a .txt file... forbidden="txt" if grep -q "${forbidden}" <<< "${1}" ; then # if extension has "txt." in it, remove "txt." from extension extension=$(echo "${extension}" | sed -r "s/^$forbidden.//g") fi # Simply copy and paste, changing extension string and extraction method below if [ "${extension}" == "tar" ] ; then dependencycheck tar echo "${txtblue}decompressing .tar...${txtyellow}" # main extraction method tar xf "${1}" echo ".tar decompression finished!${txtwhite}" fi if [ "${extension}" == "tgz" ] ; then dependencycheck tar gunzip echo "${txtblue}decompressing .tgz...${txtyellow}" # main extraction method tar zxf "${1}" echo ".tgz decompression finished!${txtwhite}" fi if [ "${extension}" == "gz" ] ; then dependencycheck gunzip echo "${txtblue}decompressing .gz...${txtyellow}" # main extraction method gunzip --keep "${1}" echo ".gz decompression finished!${txtwhite}" fi