373468543f3a521e5c9e8a3b7994c8dd7365a252
2 # Desc: Template to check that apps, files, or dirs exist.
4 #==BEGIN Define script parameters==
5 declare -Ag appRollCall
# Associative array for storing app status
6 declare -Ag fileRollCall
# Associative array for storing file status
7 declare -Ag dirRollCall
# Associative array for storing dir status
8 #==END Define script parameters==
10 #===BEGIN Declare local script functions===
12 # Desc: If arg is a command, save result in assoc array 'appRollCall'
13 # Usage: checkapp arg1 arg2 arg3 ...
15 # Input: global assoc. array 'appRollCall'
16 # Output: adds/updates key(value) to global assoc array 'appRollCall'
18 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
19 #echo "DEBUG:args: $@"
20 #echo "DEBUG:returnState:$returnState"
24 #echo "DEBUG:processing arg:$arg"
25 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
26 appRollCall
[$arg]="true";
27 #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
28 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
30 appRollCall
[$arg]="false"; returnState
="false";
34 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
35 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
37 #===Determine function return code===
38 if [ "$returnState" = "true" ]; then
39 #echo "DEBUG:checkapp returns true for $arg";
42 #echo "DEBUG:checkapp returns false for $arg";
45 } # Check that app exists
47 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
48 # 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 ...
83 # Input: global assoc. array 'dirRollCall'
84 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
85 # Output: returns 0 if app found, 1 otherwise
90 #echo "DEBUG:processing arg:$arg"
91 if [ -d "$arg" ]; then
92 dirRollCall
["$arg"]="true";
93 #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
94 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
96 dirRollCall
["$arg"]="false"; returnState
="false";
100 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
101 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
103 #===Determine function return code===
104 if [ "$returnState" = "true" ]; then
105 #echo "DEBUG:checkapp returns true for $arg";
108 #echo "DEBUG:checkapp returns false for $arg";
111 } # Check that dir exists
113 # Desc: Displays missing apps, files, and dirs
114 # Usage: displayMissing
116 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
117 # Output: stderr: messages indicating missing apps, file, or dirs
118 # Depends: bash 5, checkAppFileDir()
119 #==BEGIN Display errors==
120 #===BEGIN Display Missing Apps===
121 missingApps
="Missing apps :"
122 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
123 for key
in "${!appRollCall[@]}"; do
124 value
="${appRollCall[$key]}"
125 if [ "$value" = "false" ]; then
126 #echo "DEBUG:Missing apps: $key => $value";
127 missingApps
="$missingApps""$key "
131 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
132 echo "$missingApps" 1>&2;
134 #===END Display Missing Apps===
136 #===BEGIN Display Missing Files===
137 missingFiles
="Missing files:"
138 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
139 for key
in "${!fileRollCall[@]}"; do
140 value
="${fileRollCall[$key]}"
141 if [ "$value" = "false" ]; then
142 #echo "DEBUG:Missing files: $key => $value";
143 missingFiles
="$missingFiles""$key "
147 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
148 echo "$missingFiles" 1>&2;
150 #===END Display Missing Files===
152 #===BEGIN Display Missing Directories===
153 missingDirs
="Missing dirs:"
154 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
155 for key
in "${!dirRollCall[@]}"; do
156 value
="${dirRollCall[$key]}"
157 if [ "$value" = "false" ]; then
158 #echo "DEBUG:Missing dirs: $key => $value";
159 missingDirs
="$missingDirs""$key "
163 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
164 echo "$missingDirs" 1>&2;
166 #===END Display Missing Directories===
168 #==END Display errors==
169 } # Display missing apps, files, dirs
171 #===END Declare local script functions===
172 #==END Define script parameters==
174 #==BEGIN sample code==
175 if checkapp
cat; then echo "cat found."; fi;
176 if checkapp gpg
; then echo "gpg found."; fi;
177 if checkapp emaaaacs
; then echo "emaaaacs found."; fi;
182 # Author: Steven Baltaktei Sandoval