2 # Desc: Checks that apps, files, or dirs exist.
4 #==BEGIN Define script parameters==
5 #===BEGIN Define variables===
6 declare -Ag appRollCall
# Associative array for storing app status
7 declare -Ag fileRollCall
# Associative array for storing file status
8 declare -Ag dirRollCall
# Associative array for storing dir status
9 #===END Define variables===
11 #===BEGIN Declare local script functions===
13 # Desc: If arg is a command, save result in assoc array 'appRollCall'
14 # Usage: checkapp arg1 arg2 arg3 ...
16 # Input: global assoc. array 'appRollCall'
17 # Output: adds/updates key(value) to global assoc array 'appRollCall'
23 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
24 appRollCall
[$arg]="true";
25 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
27 appRollCall
[$arg]="false"; returnState
="false";
31 #===Determine function return code===
32 if [ "$returnState" = "true" ]; then
37 } # Check that app exists
39 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
40 # Usage: checkfile arg1 arg2 arg3 ...
42 # Input: global assoc. array 'fileRollCall'
43 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
44 # Output: returns 0 if app found, 1 otherwise
50 if [ -f "$arg" ]; then
51 fileRollCall
["$arg"]="true";
52 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
53 elif [ -z "$arg" ]; then
54 fileRollCall
["(no name)"]="false"; returnState
="false";
56 fileRollCall
["$arg"]="false"; returnState
="false";
60 #===Determine function return code===
61 if [ "$returnState" = "true" ]; then
66 } # Check that file exists
68 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
69 # Usage: checkdir arg1 arg2 arg3 ...
71 # Input: global assoc. array 'dirRollCall'
72 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
73 # Output: returns 0 if all args are dirs; 1 otherwise
79 if [ -z "$arg" ]; then
80 dirRollCall
["(Unspecified Dirname(s))"]="false"; returnState
="false";
81 elif [ -d "$arg" ]; then
82 dirRollCall
["$arg"]="true";
83 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
85 dirRollCall
["$arg"]="false"; returnState
="false";
89 #===Determine function return code===
90 if [ "$returnState" = "true" ]; then
95 } # Check that dir exists
97 # Desc: Displays missing apps, files, and dirs
98 # Usage: displayMissing
100 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
101 # Output: stderr: messages indicating missing apps, file, or dirs
102 # Output: returns exit code 0 if nothing missing; 1 otherwise
103 # Depends: bash 5, checkAppFileDir()
104 local missingApps value appMissing missingFiles fileMissing
105 local missingDirs dirMissing
107 #==BEGIN Display errors==
108 #===BEGIN Display Missing Apps===
109 missingApps
="Missing apps :";
110 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
111 for key
in "${!appRollCall[@]}"; do
112 value
="${appRollCall[$key]}";
113 if [ "$value" = "false" ]; then
114 #echo "DEBUG:Missing apps: $key => $value";
115 missingApps
="$missingApps""$key ";
119 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
120 echo "$missingApps" 1>&2;
123 #===END Display Missing Apps===
125 #===BEGIN Display Missing Files===
126 missingFiles
="Missing files:";
127 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
128 for key
in "${!fileRollCall[@]}"; do
129 value
="${fileRollCall[$key]}";
130 if [ "$value" = "false" ]; then
131 #echo "DEBUG:Missing files: $key => $value";
132 missingFiles
="$missingFiles""$key ";
136 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
137 echo "$missingFiles" 1>&2;
140 #===END Display Missing Files===
142 #===BEGIN Display Missing Directories===
143 missingDirs
="Missing dirs:";
144 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
145 for key
in "${!dirRollCall[@]}"; do
146 value
="${dirRollCall[$key]}";
147 if [ "$value" = "false" ]; then
148 #echo "DEBUG:Missing dirs: $key => $value";
149 missingDirs
="$missingDirs""$key ";
153 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
154 echo "$missingDirs" 1>&2;
157 #===END Display Missing Directories===
159 #==END Display errors==
160 #==BEGIN Determine function return code===
161 if [ "$appMissing" == "true" ] ||
[ "$fileMissing" == "true" ] ||
[ "$dirMissing" == "true" ]; then
166 #==END Determine function return code===
167 } # Display missing apps, files, dirs
169 #===END Declare local script functions===
170 #==END Define script parameters==
172 #==BEGIN sample code==
173 if checkapp
cat; then echo "cat found."; fi;
174 if checkapp gpg
; then echo "gpg found."; fi;
175 if displayMissing
1>/dev
/null
2>&1; then
176 echo "Nothing missing so far...";
179 if checkapp emaaaacs
; then echo "emaaaacs found."; fi;
181 if ! displayMissing
1>/dev
/null
2>&1; then
182 echo "Something is missing!";
186 myFile
=""; if ! checkfile
"$myFile"; then echo "An empty argument provided:$myFile"; displayMissing
; fi;
189 # Author: Steven Baltaktei Sandoval