feat(user/bkots):Use subshells to speed up jobs
[BK-2020-03.git] / unitproc / bkt-checkAppFileDir
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Checks that apps, files, or dirs exist.
3
4#==BEGIN Define script parameters==
5#===BEGIN Define variables===
6declare -Ag appRollCall # Associative array for storing app status
7declare -Ag fileRollCall # Associative array for storing file status
8declare -Ag dirRollCall # Associative array for storing dir status
9#===END Define variables===
10
11#===BEGIN Declare local script functions===
12checkapp() {
13 # Desc: If arg is a command, save result in assoc array 'appRollCall'
14 # Usage: checkapp arg1 arg2 arg3 ...
15 # Version: 0.1.1
16 # Input: global assoc. array 'appRollCall'
17 # Output: adds/updates key(value) to global assoc array 'appRollCall'
18 # Depends: bash 5.0.3
19 local returnState
20
21 #===Process Args===
22 for arg in "$@"; do
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;
26 else
27 appRollCall[$arg]="false"; returnState="false";
28 fi;
29 done;
30
31 #===Determine function return code===
32 if [ "$returnState" = "true" ]; then
33 return 0;
34 else
35 return 1;
36 fi;
37} # Check that app exists
38checkfile() {
39 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
40 # Usage: checkfile arg1 arg2 arg3 ...
41 # Version: 0.1.2
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
45 # Depends: bash 5.0.3
46 local returnState
47
48 #===Process Args===
49 for arg in "$@"; do
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";
55 else
56 fileRollCall["$arg"]="false"; returnState="false";
57 fi;
58 done;
59
60 #===Determine function return code===
61 if [ "$returnState" = "true" ]; then
62 return 0;
63 else
64 return 1;
65 fi;
66} # Check that file exists
67checkdir() {
68 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
69 # Usage: checkdir arg1 arg2 arg3 ...
70 # Version 0.1.2
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
74 # Depends: Bash 5.0.3
75 local returnState
76
77 #===Process Args===
78 for arg in "$@"; do
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
84 else
85 dirRollCall["$arg"]="false"; returnState="false";
86 fi
87 done
88
89 #===Determine function return code===
90 if [ "$returnState" = "true" ]; then
91 return 0;
92 else
93 return 1;
94 fi
95} # Check that dir exists
96displayMissing() {
97 # Desc: Displays missing apps, files, and dirs
98 # Usage: displayMissing
99 # Version 1.0.0
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
106
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 ";
116 appMissing="true";
117 fi;
118 done;
119 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
120 echo "$missingApps" 1>&2;
121 fi;
122 unset value;
123 #===END Display Missing Apps===
124
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 ";
133 fileMissing="true";
134 fi;
135 done;
136 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
137 echo "$missingFiles" 1>&2;
138 fi;
139 unset value;
140 #===END Display Missing Files===
141
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 ";
150 dirMissing="true";
151 fi;
152 done;
153 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
154 echo "$missingDirs" 1>&2;
155 fi;
156 unset value;
157 #===END Display Missing Directories===
158
159 #==END Display errors==
160 #==BEGIN Determine function return code===
161 if [ "$appMissing" == "true" ] || [ "$fileMissing" == "true" ] || [ "$dirMissing" == "true" ]; then
162 return 1;
163 else
164 return 0;
165 fi
166 #==END Determine function return code===
167} # Display missing apps, files, dirs
168
169#===END Declare local script functions===
170#==END Define script parameters==
171
172#==BEGIN sample code==
173if checkapp cat; then echo "cat found."; fi;
174if checkapp gpg; then echo "gpg found."; fi;
175if displayMissing 1>/dev/null 2>&1; then
176 echo "Nothing missing so far...";
177fi;
178
179if checkapp emaaaacs; then echo "emaaaacs found."; fi;
180sleep 1;
181if ! displayMissing 1>/dev/null 2>&1; then
182 echo "Something is missing!";
183 displayMissing;
184fi;
185sleep 1;
186myFile=""; if ! checkfile "$myFile"; then echo "An empty argument provided:$myFile"; displayMissing; fi;
187#==END sample code==
188
189# Author: Steven Baltaktei Sandoval
190# License: GPLv3+