♻️ Refactored dependencychecks in scripts

This commit is contained in:
z3rOR0ne 2022-09-04 03:31:21 -07:00
parent eee4b7ba43
commit 5ae285d176
3 changed files with 68 additions and 71 deletions

View file

@ -3,6 +3,10 @@
# 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)
@ -11,24 +15,28 @@ txtred=${txtbld}$(tput setaf 1)
txtyellow=${txtbld}$(tput setaf 3)
txtwhite=${txtbld}$(tput setaf 7)
# Dependency check
function dependencycheck()
{
numdependencies="$#"
dependencies=("$@")
missingdependencies=0
# To be implemented:
# dependencies=("echo" "sed" "tar" "zip" "bzip2" "gzip" "gunzip" "pax" "7z" "ar" "cpio" "brotli" "lzip" "rzip" "xz" "zstd")
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
#Use for loop to check if recognized file formats have been passed to first argument
if [ $missingdependencies -gt 0 ] ; then
echo "${txtred}Please install needed dependencies${txtwhite}"
exit 1
fi
}
# Main Dependency check
dependencies=("echo" "sed" "tput")
numdependencies=3
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
dependencycheck echo sed
# Intro Prompt
echo "${txtgreen}xtract: a simple shell script for decompressing common file formats"
@ -41,24 +49,7 @@ extension=$(echo "${1}" | sed 's/^[^\..:]*[\..]//')
# Simply copy and paste, changing extension string and extraction method below
if [ "${extension}" == "tgz" ] ; then
# Dependency check
dependencies=("echo" "tar" "gunzip")
numdependencies=3
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
dependencycheck tar gunzip
echo "${txtblue}decompressing .tgz...${txtyellow}"
# main extraction method
tar zxvf "${1}" &> /dev/null