3 # Desc: Template to check that apps, files, or dirs exist.
4 # Author: Steven Baltaktei Sandoval
7 #==BEGIN Define script parameters==
8 declare -Ag appRollCall
# Associative array for storing app status
9 declare -Ag fileRollCall
# Associative array for storing file status
10 declare -Ag dirRollCall
# Associative array for storing dir status
12 #===BEGIN Declare local script functions===
14 # Desc: If arg is a command, save result in assoc array 'appRollCall'
15 # Usage: checkapp arg1 arg2 arg3 ...
16 # Input: global assoc. array 'appRollCall'
17 # Output: adds/updates key(value) to global assoc array 'appRollCall'
19 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
20 #echo "DEBUG:args: $@"
21 #echo "DEBUG:returnState:$returnState"
25 #echo "DEBUG:processing arg:$arg"
26 if command -v $arg 1>/dev
/null
2>&1; then # Check if arg is a valid command
27 appRollCall
[$arg]="true";
28 #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
29 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
31 appRollCall
[$arg]="false"; returnState
="false";
35 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
36 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
38 #===Determine function return code===
39 if [ "$returnState" = "true" ]; then
40 #echo "DEBUG:checkapp returns true for $arg";
43 #echo "DEBUG:checkapp returns false for $arg";
46 } # Check that app exists
48 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
49 # Usage: checkfile arg1 arg2 arg3 ...
50 # Input: global assoc. array 'fileRollCall'
51 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
52 # Output: returns 0 if app found, 1 otherwise
57 #echo "DEBUG:processing arg:$arg"
58 if [ -f "$arg" ]; then
59 fileRollCall
["$arg"]="true";
60 #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]}
61 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
63 fileRollCall
["$arg"]="false"; returnState
="false";
67 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
68 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
70 #===Determine function return code===
71 if [ "$returnState" = "true" ]; then
72 #echo "DEBUG:checkapp returns true for $arg";
75 #echo "DEBUG:checkapp returns false for $arg";
78 } # Check that file exists
80 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
81 # Usage: checkdir arg1 arg2 arg3 ...
82 # Input: global assoc. array 'dirRollCall'
83 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
84 # Output: returns 0 if app found, 1 otherwise
89 #echo "DEBUG:processing arg:$arg"
90 if [ -d "$arg" ]; then
91 dirRollCall
["$arg"]="true";
92 #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
93 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
95 dirRollCall
["$arg"]="false"; returnState
="false";
99 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
100 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
102 #===Determine function return code===
103 if [ "$returnState" = "true" ]; then
104 #echo "DEBUG:checkapp returns true for $arg";
107 #echo "DEBUG:checkapp returns false for $arg";
110 } # Check that dir exists
111 #===END Declare local script functions===
112 #==END Define script parameters==
114 #==BEGIN sample code==
115 if checkapp
cat; then echo "cat found."; fi
116 if checkapp gpg
; then echo "gpg found."; fi
117 if checkapp emaaaacs
; then echo "emaaaacs found."; fi
120 #==BEGIN Display errors==
121 #===BEGIN Display Missing Apps===
122 missingApps
="Missing apps :"
123 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
124 for key
in "${!appRollCall[@]}"; do
125 value
="${appRollCall[$key]}"
126 if [ "$value" = "false" ]; then
127 #echo "DEBUG:Missing apps: $key => $value";
128 missingApps
="$missingApps""$key "
132 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
133 echo "$missingApps" 1>&2;
135 #===END Display Missing Apps===
137 #===BEGIN Display Missing Files===
138 missingFiles
="Missing files:"
139 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
140 for key
in "${!fileRollCall[@]}"; do
141 value
="${fileRollCall[$key]}"
142 if [ "$value" = "false" ]; then
143 #echo "DEBUG:Missing files: $key => $value";
144 missingFiles
="$missingFiles""$key "
148 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
149 echo "$missingFiles" 1>&2;
151 #===END Display Missing Files===
153 #===BEGIN Display Missing Directories===
154 missingDirs
="Missing dirs:"
155 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
156 for key
in "${!dirRollCall[@]}"; do
157 value
="${dirRollCall[$key]}"
158 if [ "$value" = "false" ]; then
159 #echo "DEBUG:Missing dirs: $key => $value";
160 missingDirs
="$missingDirs""$key "
164 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
165 echo "$missingDirs" 1>&2;
167 #===END Display Missing Directories===
169 #===BEGIN Display Failed Functions===
170 if [ ${#failedFunctions} -gt 0 ]; then # Only indicate if an function failed.
171 echo "Failed functions:${failedFunctions[@]}" 1>&2;
173 #===END Display Failed Functions===
175 #==END Display errors==