#!/bin/bash
-
-# Desc: Template to check that apps, files, or dirs exist.
-# Author: Steven Baltaktei Sandoval
-# License: GPLv3+
+# Desc: Checks that apps, files, or dirs exist.
#==BEGIN Define script parameters==
+#===BEGIN Define variables===
declare -Ag appRollCall # Associative array for storing app status
declare -Ag fileRollCall # Associative array for storing file status
declare -Ag dirRollCall # Associative array for storing dir status
+#===END Define variables===
#===BEGIN Declare local script functions===
checkapp() {
# Desc: If arg is a command, save result in assoc array 'appRollCall'
# Usage: checkapp arg1 arg2 arg3 ...
+ # Version: 0.1.1
# Input: global assoc. array 'appRollCall'
# Output: adds/updates key(value) to global assoc array 'appRollCall'
+ # Depends: bash 5.0.3
local returnState
- #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
- #echo "DEBUG:args: $@"
- #echo "DEBUG:returnState:$returnState"
#===Process Args===
for arg in "$@"; do
- #echo "DEBUG:processing arg:$arg"
if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
appRollCall[$arg]="true";
- #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
- if ! [ "$returnState" = "false" ]; then returnState="true"; fi
+ if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
else
appRollCall[$arg]="false"; returnState="false";
- fi
- done
-
- #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
- #echo "DEBUG:evaluating returnstate. returnState:"$returnState
+ fi;
+ done;
#===Determine function return code===
if [ "$returnState" = "true" ]; then
- #echo "DEBUG:checkapp returns true for $arg";
return 0;
else
- #echo "DEBUG:checkapp returns false for $arg";
return 1;
- fi
+ fi;
} # Check that app exists
checkfile() {
# Desc: If arg is a file path, save result in assoc array 'fileRollCall'
# Usage: checkfile arg1 arg2 arg3 ...
+ # Version: 0.1.1
# Input: global assoc. array 'fileRollCall'
# Output: adds/updates key(value) to global assoc array 'fileRollCall';
# Output: returns 0 if app found, 1 otherwise
+ # Depends: bash 5.0.3
local returnState
#===Process Args===
for arg in "$@"; do
- #echo "DEBUG:processing arg:$arg"
if [ -f "$arg" ]; then
fileRollCall["$arg"]="true";
- #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]}
- if ! [ "$returnState" = "false" ]; then returnState="true"; fi
+ if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
else
fileRollCall["$arg"]="false"; returnState="false";
- fi
- done
-
- #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
- #echo "DEBUG:evaluating returnstate. returnState:"$returnState
+ fi;
+ done;
#===Determine function return code===
if [ "$returnState" = "true" ]; then
- #echo "DEBUG:checkapp returns true for $arg";
return 0;
else
- #echo "DEBUG:checkapp returns false for $arg";
return 1;
- fi
+ fi;
} # Check that file exists
checkdir() {
# Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
# Usage: checkdir arg1 arg2 arg3 ...
+ # Version 0.1.2
# Input: global assoc. array 'dirRollCall'
# Output: adds/updates key(value) to global assoc array 'dirRollCall';
- # Output: returns 0 if app found, 1 otherwise
+ # Output: returns 0 if all args are dirs; 1 otherwise
+ # Depends: Bash 5.0.3
local returnState
#===Process Args===
for arg in "$@"; do
- #echo "DEBUG:processing arg:$arg"
- if [ -d "$arg" ]; then
+ if [ -z "$arg" ]; then
+ dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false";
+ elif [ -d "$arg" ]; then
dirRollCall["$arg"]="true";
- #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
if ! [ "$returnState" = "false" ]; then returnState="true"; fi
else
dirRollCall["$arg"]="false"; returnState="false";
fi
done
-
- #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
- #echo "DEBUG:evaluating returnstate. returnState:"$returnState
#===Determine function return code===
if [ "$returnState" = "true" ]; then
- #echo "DEBUG:checkapp returns true for $arg";
return 0;
else
- #echo "DEBUG:checkapp returns false for $arg";
return 1;
fi
} # Check that dir exists
-#===END Declare local script functions===
-#==END Define script parameters==
+displayMissing() {
+ # Desc: Displays missing apps, files, and dirs
+ # Usage: displayMissing
+ # Version 0.1.1
+ # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
+ # Output: stderr: messages indicating missing apps, file, or dirs
+ # Depends: bash 5, checkAppFileDir()
+ local missingApps value appMissing missingFiles fileMissing
+ local missingDirs dirMissing
+
+ #==BEGIN Display errors==
+ #===BEGIN Display Missing Apps===
+ missingApps="Missing apps :";
+ #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
+ for key in "${!appRollCall[@]}"; do
+ value="${appRollCall[$key]}";
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing apps: $key => $value";
+ missingApps="$missingApps""$key ";
+ appMissing="true";
+ fi;
+ done;
+ if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingApps" 1>&2;
+ fi;
+ unset value;
+ #===END Display Missing Apps===
-#==BEGIN sample code==
-if checkapp cat; then echo "cat found."; fi
-if checkapp gpg; then echo "gpg found."; fi
-if checkapp emaaaacs; then echo "emaaaacs found."; fi
-#==END sample code==
+ #===BEGIN Display Missing Files===
+ missingFiles="Missing files:";
+ #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
+ for key in "${!fileRollCall[@]}"; do
+ value="${fileRollCall[$key]}";
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing files: $key => $value";
+ missingFiles="$missingFiles""$key ";
+ fileMissing="true";
+ fi;
+ done;
+ if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingFiles" 1>&2;
+ fi;
+ unset value;
+ #===END Display Missing Files===
-#==BEGIN Display errors==
-#===BEGIN Display Missing Apps===
-missingApps="Missing apps :"
-#for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
-for key in "${!appRollCall[@]}"; do
- value="${appRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing apps: $key => $value";
- missingApps="$missingApps""$key "
- appMissing="true"
- fi
-done
-if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
- echo "$missingApps" 1>&2;
-fi
-#===END Display Missing Apps===
+ #===BEGIN Display Missing Directories===
+ missingDirs="Missing dirs:";
+ #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
+ for key in "${!dirRollCall[@]}"; do
+ value="${dirRollCall[$key]}";
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing dirs: $key => $value";
+ missingDirs="$missingDirs""$key ";
+ dirMissing="true";
+ fi;
+ done;
+ if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
+ echo "$missingDirs" 1>&2;
+ fi;
+ unset value;
+ #===END Display Missing Directories===
-#===BEGIN Display Missing Files===
-missingFiles="Missing files:"
-#for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
-for key in "${!fileRollCall[@]}"; do
- value="${fileRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing files: $key => $value";
- missingFiles="$missingFiles""$key "
- fileMissing="true"
- fi
-done
-if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
- echo "$missingFiles" 1>&2;
-fi
-#===END Display Missing Files===
+ #==END Display errors==
+} # Display missing apps, files, dirs
-#===BEGIN Display Missing Directories===
-missingDirs="Missing dirs:"
-#for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
-for key in "${!dirRollCall[@]}"; do
- value="${dirRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing dirs: $key => $value";
- missingDirs="$missingDirs""$key "
- dirMissing="true"
- fi
-done
-if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
- echo "$missingDirs" 1>&2;
-fi
-#===END Display Missing Directories===
+#===END Declare local script functions===
+#==END Define script parameters==
-#===BEGIN Display Failed Functions===
-if [ ${#failedFunctions} -gt 0 ]; then # Only indicate if an function failed.
- echo "Failed functions:${failedFunctions[@]}" 1>&2;
-fi
-#===END Display Failed Functions===
+#==BEGIN sample code==
+if checkapp cat; then echo "cat found."; fi;
+if checkapp gpg; then echo "gpg found."; fi;
+if checkapp emaaaacs; then echo "emaaaacs found."; fi;
+sleep 1;
+displayMissing;
+#==END sample code==
-#==END Display errors==
+# Author: Steven Baltaktei Sandoval
+# License: GPLv3+