mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-09 13:43:46 -05:00
Better sourcing of lib files
This commit is contained in:
69
lib/utils.sh
69
lib/utils.sh
@@ -15,41 +15,6 @@
|
|||||||
#
|
#
|
||||||
# ##################################################
|
# ##################################################
|
||||||
|
|
||||||
|
|
||||||
# Source additional files
|
|
||||||
# ------------------------------------------------------
|
|
||||||
# The list of additional utility files to be sourced
|
|
||||||
# ------------------------------------------------------
|
|
||||||
|
|
||||||
# First we locate this script and populate the $SCRIPTPATH variable
|
|
||||||
# Doing so allows us to source additional files from this utils file.
|
|
||||||
SOURCE="${BASH_SOURCE[0]}"
|
|
||||||
while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink
|
|
||||||
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
|
||||||
SOURCE="$(readlink "${SOURCE}")"
|
|
||||||
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
|
||||||
done
|
|
||||||
SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
|
||||||
|
|
||||||
# Write the list of utility files to be sourced
|
|
||||||
FILES="
|
|
||||||
sharedVariables.sh
|
|
||||||
sharedFunctions.sh
|
|
||||||
setupScriptFunctions.sh
|
|
||||||
"
|
|
||||||
|
|
||||||
# Source the Utility Files
|
|
||||||
for file in $FILES
|
|
||||||
do
|
|
||||||
if [ -f "${SOURCEPATH}/${file}" ]; then
|
|
||||||
source "${SOURCEPATH}/${file}"
|
|
||||||
else
|
|
||||||
error "${file} does not exist. Exiting"
|
|
||||||
Exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
# Logging and Colors
|
# Logging and Colors
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
# Here we set the colors for our script feedback.
|
# Here we set the colors for our script feedback.
|
||||||
@@ -121,6 +86,40 @@ verbose() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Source additional files
|
||||||
|
# ------------------------------------------------------
|
||||||
|
# The list of additional utility files to be sourced
|
||||||
|
# ------------------------------------------------------
|
||||||
|
|
||||||
|
# First we locate this script and populate the $SCRIPTPATH variable
|
||||||
|
# Doing so allows us to source additional files from this utils file.
|
||||||
|
SOURCE="${BASH_SOURCE[0]}"
|
||||||
|
while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink
|
||||||
|
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
||||||
|
SOURCE="$(readlink "${SOURCE}")"
|
||||||
|
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
||||||
|
done
|
||||||
|
SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
||||||
|
|
||||||
|
# Write the list of utility files to be sourced
|
||||||
|
FILES="
|
||||||
|
sharedVariables.sh
|
||||||
|
sharedFunctions.sh
|
||||||
|
setupScriptFunctions.sh
|
||||||
|
"
|
||||||
|
|
||||||
|
# Source the Utility Files
|
||||||
|
for file in $FILES
|
||||||
|
do
|
||||||
|
if [ -f "${SOURCEPATH}/${file}" ]; then
|
||||||
|
source "${SOURCEPATH}/${file}"
|
||||||
|
else
|
||||||
|
die "${file} does not exist."
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
# Notes to self
|
# Notes to self
|
||||||
# ####################
|
# ####################
|
||||||
# This is how you create a variable with multiple lines
|
# This is how you create a variable with multiple lines
|
||||||
|
|||||||
@@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -34,12 +34,14 @@ scriptTemplateVersion="1.0.1" # Version of scriptTemplate.sh
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "You must have utils.sh to run. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 0
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Confirm we have Dropbox
|
# Confirm we have Dropbox
|
||||||
hasDropbox
|
hasDropbox
|
||||||
|
|
||||||
|
|||||||
@@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -32,12 +32,14 @@ scriptTemplateVersion="1.0.1" # Version of scriptTemplate.sh
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -11,13 +11,15 @@
|
|||||||
# ##################################################
|
# ##################################################
|
||||||
|
|
||||||
# Source global utilities
|
# Source global utilities
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "You must have utils.sh to run. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
seek_confirmation "Do you want to run the Dropbox script to install it first?"
|
seek_confirmation "Do you want to run the Dropbox script to install it first?"
|
||||||
if is_confirmed; then
|
if is_confirmed; then
|
||||||
if is_file "./dropbox.sh"; then
|
if is_file "./dropbox.sh"; then
|
||||||
|
|||||||
@@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
@@ -43,13 +43,15 @@ scriptTemplateVersion="1.0.0" # Version of Script Template
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# If these can't be found, update the path to the file
|
# If these can't be found, update the path to the file
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
if [ -f "../lib/utils.sh" ]; then
|
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
source "../lib/utils.sh"
|
if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then
|
||||||
|
source "${SCRIPTDIR}/../lib/utils.sh"
|
||||||
else
|
else
|
||||||
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# trapCleanup Function
|
# trapCleanup Function
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# Any actions that should be taken if the script is prematurely
|
# Any actions that should be taken if the script is prematurely
|
||||||
|
|||||||
Reference in New Issue
Block a user