♻️ Refactored dependencychecks in scripts
This commit is contained in:
parent
eee4b7ba43
commit
5ae285d176
3 changed files with 68 additions and 71 deletions
|
|
@ -6,7 +6,6 @@ set -e
|
||||||
# For styling/colorizing output
|
# For styling/colorizing output
|
||||||
txtbld=$(tput bold)
|
txtbld=$(tput bold)
|
||||||
txtred=${txtbld}$(tput setaf 1)
|
txtred=${txtbld}$(tput setaf 1)
|
||||||
txtgreen=${txtbld}$(tput setaf 2)
|
|
||||||
txtwhite=${txtbld}$(tput setaf 7)
|
txtwhite=${txtbld}$(tput setaf 7)
|
||||||
|
|
||||||
# Dependency check
|
# Dependency check
|
||||||
|
|
@ -17,8 +16,7 @@ function dependencycheck()
|
||||||
missingdependencies=0
|
missingdependencies=0
|
||||||
|
|
||||||
for ((i = 0; i < numdependencies; i++)) ; do
|
for ((i = 0; i < numdependencies; i++)) ; do
|
||||||
if ! command -v "${dependencies[$i]}" &> /dev/null
|
if ! command -v "${dependencies[$i]}" &> /dev/null ; then
|
||||||
then
|
|
||||||
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
||||||
missingdependencies=$((missingdependencies+1))
|
missingdependencies=$((missingdependencies+1))
|
||||||
fi
|
fi
|
||||||
|
|
@ -27,11 +25,28 @@ function dependencycheck()
|
||||||
if [ $missingdependencies -gt 0 ] ; then
|
if [ $missingdependencies -gt 0 ] ; then
|
||||||
echo "${txtred}Please install needed dependencies${txtwhite}"
|
echo "${txtred}Please install needed dependencies${txtwhite}"
|
||||||
exit 1
|
exit 1
|
||||||
#comment out if sourced
|
|
||||||
else
|
|
||||||
echo "${txtgreen}All dependencies are met!${txtwhite}"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# invoke function comment out if sourced
|
# invoke function comment out if sourced
|
||||||
dependencycheck "$@"
|
dependencycheck "$@"
|
||||||
|
|
||||||
|
# Barebones version without tput (for being sourced):
|
||||||
|
# function dependencycheck()
|
||||||
|
# {
|
||||||
|
# numdependencies="$#"
|
||||||
|
# dependencies=("$@")
|
||||||
|
# missingdependencies=0
|
||||||
|
|
||||||
|
# for ((i = 0; i < numdependencies; i++)) ; do
|
||||||
|
# if ! command -v "${dependencies[$i]}" &> /dev/null ; then
|
||||||
|
# echo "dependency not met: ${dependencies[$i]}"
|
||||||
|
# missingdependencies=$((missingdependencies+1))
|
||||||
|
# fi
|
||||||
|
# done
|
||||||
|
|
||||||
|
# if [ $missingdependencies -gt 0 ] ; then
|
||||||
|
# echo "Please install needed dependencies"
|
||||||
|
# exit 1
|
||||||
|
# fi
|
||||||
|
# }
|
||||||
|
|
|
||||||
|
|
@ -3,33 +3,41 @@
|
||||||
# Error handling
|
# Error handling
|
||||||
set -e
|
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
|
# For styling/colorizing output
|
||||||
txtbld=$(tput bold)
|
txtbld=$(tput bold)
|
||||||
txtblue=${txtbld}$(tput setaf 4)
|
txtblue=${txtbld}$(tput setaf 4)
|
||||||
txtgreen=${txtbld}$(tput setaf 2)
|
|
||||||
txtred=${txtbld}$(tput setaf 1)
|
txtred=${txtbld}$(tput setaf 1)
|
||||||
|
txtgreen=${txtbld}$(tput setaf 2)
|
||||||
txtyellow=${txtbld}$(tput setaf 3)
|
txtyellow=${txtbld}$(tput setaf 3)
|
||||||
txtwhite=${txtbld}$(tput setaf 7)
|
txtwhite=${txtbld}$(tput setaf 7)
|
||||||
|
|
||||||
|
# Dependency check
|
||||||
# To be implemented:
|
function dependencycheck()
|
||||||
# dependencies=("echo" "sed" "tar" "zip" "bzip2" "gzip" "gunzip" "pax" "7z" "ar" "cpio" "brotli" "lzip" "rzip" "xz" "zstd")
|
{
|
||||||
|
numdependencies="$#"
|
||||||
#Use for loop to check if recognized file formats have been passed to first argument
|
dependencies=("$@")
|
||||||
|
|
||||||
# Main Dependency check
|
|
||||||
dependencies=("echo" "sed" "tput")
|
|
||||||
numdependencies=3
|
|
||||||
missingdependencies=0
|
missingdependencies=0
|
||||||
for ((i = 0; i < numdependencies; i++)) ;
|
|
||||||
do
|
for ((i = 0; i < numdependencies; i++)) ; do
|
||||||
if ! command -v "${dependencies[$i]}" &> /dev/null
|
if ! command -v "${dependencies[$i]}" &> /dev/null ; then
|
||||||
then
|
|
||||||
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
||||||
missingdependencies=$((missingdependencies+1))
|
missingdependencies=$((missingdependencies+1))
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $missingdependencies -gt 0 ] ; then
|
||||||
|
echo "${txtred}Please install needed dependencies${txtwhite}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main Dependency check
|
||||||
|
dependencycheck echo sed
|
||||||
|
|
||||||
# Intro Prompt
|
# Intro Prompt
|
||||||
echo "${txtgreen}xpress: a simple shell script for compressing common file formats"
|
echo "${txtgreen}xpress: a simple shell script for compressing common file formats"
|
||||||
|
|
||||||
|
|
@ -44,24 +52,7 @@ fdname=$(echo "${1}" | sed 's/\..*$//')
|
||||||
|
|
||||||
# Simply copy and paste, changing extension string and extraction method below
|
# Simply copy and paste, changing extension string and extraction method below
|
||||||
if [ "${extension}" == "tgz" ] ; then
|
if [ "${extension}" == "tgz" ] ; then
|
||||||
# Dependency check
|
dependencycheck tar gzip
|
||||||
dependencies=("echo" "tar" "gzip")
|
|
||||||
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
|
|
||||||
|
|
||||||
echo "${txtblue}compressing .tgz...${txtyellow}"
|
echo "${txtblue}compressing .tgz...${txtyellow}"
|
||||||
# main extraction method
|
# main extraction method
|
||||||
tar -cvzf "${1}" "${fdname}" &> /dev/null
|
tar -cvzf "${1}" "${fdname}" &> /dev/null
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@
|
||||||
# Error handling
|
# Error handling
|
||||||
set -e
|
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
|
# For styling/colorizing output
|
||||||
txtbld=$(tput bold)
|
txtbld=$(tput bold)
|
||||||
txtblue=${txtbld}$(tput setaf 4)
|
txtblue=${txtbld}$(tput setaf 4)
|
||||||
|
|
@ -11,25 +15,29 @@ txtred=${txtbld}$(tput setaf 1)
|
||||||
txtyellow=${txtbld}$(tput setaf 3)
|
txtyellow=${txtbld}$(tput setaf 3)
|
||||||
txtwhite=${txtbld}$(tput setaf 7)
|
txtwhite=${txtbld}$(tput setaf 7)
|
||||||
|
|
||||||
|
# Dependency check
|
||||||
# To be implemented:
|
function dependencycheck()
|
||||||
# dependencies=("echo" "sed" "tar" "zip" "bzip2" "gzip" "gunzip" "pax" "7z" "ar" "cpio" "brotli" "lzip" "rzip" "xz" "zstd")
|
{
|
||||||
|
numdependencies="$#"
|
||||||
#Use for loop to check if recognized file formats have been passed to first argument
|
dependencies=("$@")
|
||||||
|
|
||||||
# Main Dependency check
|
|
||||||
dependencies=("echo" "sed" "tput")
|
|
||||||
numdependencies=3
|
|
||||||
missingdependencies=0
|
missingdependencies=0
|
||||||
for ((i = 0; i < numdependencies; i++)) ;
|
|
||||||
do
|
for ((i = 0; i < numdependencies; i++)) ; do
|
||||||
if ! command -v "${dependencies[$i]}" &> /dev/null
|
if ! command -v "${dependencies[$i]}" &> /dev/null ; then
|
||||||
then
|
|
||||||
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
echo "${txtred}dependency not met: ${dependencies[$i]}${txtwhite}"
|
||||||
missingdependencies=$((missingdependencies+1))
|
missingdependencies=$((missingdependencies+1))
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $missingdependencies -gt 0 ] ; then
|
||||||
|
echo "${txtred}Please install needed dependencies${txtwhite}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main Dependency check
|
||||||
|
dependencycheck echo sed
|
||||||
|
|
||||||
# Intro Prompt
|
# Intro Prompt
|
||||||
echo "${txtgreen}xtract: a simple shell script for decompressing common file formats"
|
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
|
# Simply copy and paste, changing extension string and extraction method below
|
||||||
if [ "${extension}" == "tgz" ] ; then
|
if [ "${extension}" == "tgz" ] ; then
|
||||||
# Dependency check
|
dependencycheck tar gunzip
|
||||||
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
|
|
||||||
|
|
||||||
echo "${txtblue}decompressing .tgz...${txtyellow}"
|
echo "${txtblue}decompressing .tgz...${txtyellow}"
|
||||||
# main extraction method
|
# main extraction method
|
||||||
tar zxvf "${1}" &> /dev/null
|
tar zxvf "${1}" &> /dev/null
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue