2 # Desc: Checks 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'
22 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
23 appRollCall
[$arg]="true";
24 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
26 appRollCall
[$arg]="false"; returnState
="false";
30 #===Determine function return code===
31 if [ "$returnState" = "true" ]; then
36 } # Check that app exists
38 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
39 # Usage: checkfile arg1 arg2 arg3 ...
41 # Input: global assoc. array 'fileRollCall'
42 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
43 # Output: returns 0 if app found, 1 otherwise
49 if [ -f "$arg" ]; then
50 fileRollCall
["$arg"]="true";
51 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
53 fileRollCall
["$arg"]="false"; returnState
="false";
57 #===Determine function return code===
58 if [ "$returnState" = "true" ]; then
63 } # Check that file exists
65 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
66 # Usage: checkdir arg1 arg2 arg3 ...
68 # Input: global assoc. array 'dirRollCall'
69 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
70 # Output: returns 0 if app found, 1 otherwise
76 if [ -d "$arg" ]; then
77 dirRollCall
["$arg"]="true";
78 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
80 dirRollCall
["$arg"]="false"; returnState
="false";
84 #===Determine function return code===
85 if [ "$returnState" = "true" ]; then
90 } # Check that dir exists
92 # Desc: Displays missing apps, files, and dirs
93 # Usage: displayMissing
95 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
96 # Output: stderr: messages indicating missing apps, file, or dirs
97 # Depends: bash 5, checkAppFileDir()
98 local missingApps value appMissing missingFiles fileMissing
99 local missingDirs dirMissing
101 #==BEGIN Display errors==
102 #===BEGIN Display Missing Apps===
103 missingApps
="Missing apps :";
104 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
105 for key
in "${!appRollCall[@]}"; do
106 value
="${appRollCall[$key]}";
107 if [ "$value" = "false" ]; then
108 #echo "DEBUG:Missing apps: $key => $value";
109 missingApps
="$missingApps""$key ";
113 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
114 echo "$missingApps" 1>&2;
117 #===END Display Missing Apps===
119 #===BEGIN Display Missing Files===
120 missingFiles
="Missing files:";
121 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
122 for key
in "${!fileRollCall[@]}"; do
123 value
="${fileRollCall[$key]}";
124 if [ "$value" = "false" ]; then
125 #echo "DEBUG:Missing files: $key => $value";
126 missingFiles
="$missingFiles""$key ";
130 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
131 echo "$missingFiles" 1>&2;
134 #===END Display Missing Files===
136 #===BEGIN Display Missing Directories===
137 missingDirs
="Missing dirs:";
138 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
139 for key
in "${!dirRollCall[@]}"; do
140 value
="${dirRollCall[$key]}";
141 if [ "$value" = "false" ]; then
142 #echo "DEBUG:Missing dirs: $key => $value";
143 missingDirs
="$missingDirs""$key ";
147 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
148 echo "$missingDirs" 1>&2;
151 #===END Display Missing Directories===
153 #==END Display errors==
154 } # Display missing apps, files, dirs
156 #===END Declare local script functions===
157 #==END Define script parameters==
159 #==BEGIN sample code==
160 if checkapp
cat; then echo "cat found."; fi;
161 if checkapp gpg
; then echo "gpg found."; fi;
162 if checkapp emaaaacs
; then echo "emaaaacs found."; fi;
167 # Author: Steven Baltaktei Sandoval