chore(user/bkmml):Cleanup comment
[BK-2020-03.git] / unitproc / bkt-checkAppFileDir
CommitLineData
65567c48 1#!/bin/bash
3d2d59ee 2# Desc: Checks that apps, files, or dirs exist.
9367c58e 3
65567c48 4#==BEGIN Define script parameters==
2928a8e4 5#===BEGIN Define variables===
65567c48
SBS
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
2928a8e4 9#===END Define variables===
65567c48
SBS
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 ...
3d2d59ee 15 # Version: 0.1.1
65567c48
SBS
16 # Input: global assoc. array 'appRollCall'
17 # Output: adds/updates key(value) to global assoc array 'appRollCall'
3d2d59ee 18 # Depends: bash 5.0.3
65567c48 19 local returnState
65567c48
SBS
20
21 #===Process Args===
22 for arg in "$@"; do
0733880e 23 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
65567c48 24 appRollCall[$arg]="true";
3d2d59ee 25 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
65567c48
SBS
26 else
27 appRollCall[$arg]="false"; returnState="false";
3d2d59ee
SBS
28 fi;
29 done;
65567c48
SBS
30
31 #===Determine function return code===
32 if [ "$returnState" = "true" ]; then
65567c48
SBS
33 return 0;
34 else
65567c48 35 return 1;
3d2d59ee 36 fi;
65567c48
SBS
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 ...
d7ffba91 41 # Version: 0.1.2
65567c48
SBS
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
3d2d59ee 45 # Depends: bash 5.0.3
65567c48
SBS
46 local returnState
47
48 #===Process Args===
49 for arg in "$@"; do
65567c48
SBS
50 if [ -f "$arg" ]; then
51 fileRollCall["$arg"]="true";
3d2d59ee 52 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
d7ffba91
SBS
53 elif [ -z "$arg" ]; then
54 fileRollCall["(no name)"]="false"; returnState="false";
65567c48
SBS
55 else
56 fileRollCall["$arg"]="false"; returnState="false";
3d2d59ee
SBS
57 fi;
58 done;
65567c48
SBS
59
60 #===Determine function return code===
61 if [ "$returnState" = "true" ]; then
65567c48
SBS
62 return 0;
63 else
65567c48 64 return 1;
3d2d59ee 65 fi;
65567c48
SBS
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 ...
b700f6ff 70 # Version 0.1.2
65567c48
SBS
71 # Input: global assoc. array 'dirRollCall'
72 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
b700f6ff 73 # Output: returns 0 if all args are dirs; 1 otherwise
3d2d59ee 74 # Depends: Bash 5.0.3
65567c48
SBS
75 local returnState
76
77 #===Process Args===
78 for arg in "$@"; do
b700f6ff
SBS
79 if [ -z "$arg" ]; then
80 dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false";
81 elif [ -d "$arg" ]; then
65567c48 82 dirRollCall["$arg"]="true";
65567c48
SBS
83 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
84 else
85 dirRollCall["$arg"]="false"; returnState="false";
86 fi
87 done
65567c48
SBS
88
89 #===Determine function return code===
90 if [ "$returnState" = "true" ]; then
65567c48
SBS
91 return 0;
92 else
65567c48
SBS
93 return 1;
94 fi
95} # Check that dir exists
61b9303c
SBS
96displayMissing() {
97 # Desc: Displays missing apps, files, and dirs
98 # Usage: displayMissing
b8f798aa 99 # Version 1.0.0
61b9303c
SBS
100 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
101 # Output: stderr: messages indicating missing apps, file, or dirs
b8f798aa 102 # Output: returns exit code 0 if nothing missing; 1 otherwise
61b9303c 103 # Depends: bash 5, checkAppFileDir()
3d2d59ee
SBS
104 local missingApps value appMissing missingFiles fileMissing
105 local missingDirs dirMissing
b8f798aa 106
61b9303c
SBS
107 #==BEGIN Display errors==
108 #===BEGIN Display Missing Apps===
3d2d59ee 109 missingApps="Missing apps :";
61b9303c
SBS
110 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
111 for key in "${!appRollCall[@]}"; do
3d2d59ee 112 value="${appRollCall[$key]}";
61b9303c
SBS
113 if [ "$value" = "false" ]; then
114 #echo "DEBUG:Missing apps: $key => $value";
3d2d59ee
SBS
115 missingApps="$missingApps""$key ";
116 appMissing="true";
117 fi;
118 done;
61b9303c
SBS
119 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
120 echo "$missingApps" 1>&2;
3d2d59ee
SBS
121 fi;
122 unset value;
61b9303c 123 #===END Display Missing Apps===
65567c48 124
61b9303c 125 #===BEGIN Display Missing Files===
3d2d59ee 126 missingFiles="Missing files:";
61b9303c
SBS
127 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
128 for key in "${!fileRollCall[@]}"; do
3d2d59ee 129 value="${fileRollCall[$key]}";
61b9303c
SBS
130 if [ "$value" = "false" ]; then
131 #echo "DEBUG:Missing files: $key => $value";
3d2d59ee
SBS
132 missingFiles="$missingFiles""$key ";
133 fileMissing="true";
134 fi;
135 done;
61b9303c
SBS
136 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
137 echo "$missingFiles" 1>&2;
3d2d59ee
SBS
138 fi;
139 unset value;
61b9303c 140 #===END Display Missing Files===
65567c48 141
61b9303c 142 #===BEGIN Display Missing Directories===
3d2d59ee 143 missingDirs="Missing dirs:";
61b9303c
SBS
144 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
145 for key in "${!dirRollCall[@]}"; do
3d2d59ee 146 value="${dirRollCall[$key]}";
61b9303c
SBS
147 if [ "$value" = "false" ]; then
148 #echo "DEBUG:Missing dirs: $key => $value";
3d2d59ee
SBS
149 missingDirs="$missingDirs""$key ";
150 dirMissing="true";
151 fi;
152 done;
61b9303c
SBS
153 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
154 echo "$missingDirs" 1>&2;
3d2d59ee
SBS
155 fi;
156 unset value;
61b9303c
SBS
157 #===END Display Missing Directories===
158
159 #==END Display errors==
b8f798aa
SBS
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===
61b9303c
SBS
167} # Display missing apps, files, dirs
168
169#===END Declare local script functions===
170#==END Define script parameters==
65567c48 171
61b9303c
SBS
172#==BEGIN sample code==
173if checkapp cat; then echo "cat found."; fi;
174if checkapp gpg; then echo "gpg found."; fi;
b8f798aa
SBS
175if displayMissing 1>/dev/null 2>&1; then
176 echo "Nothing missing so far...";
177fi;
178
61b9303c
SBS
179if checkapp emaaaacs; then echo "emaaaacs found."; fi;
180sleep 1;
b8f798aa
SBS
181if ! displayMissing 1>/dev/null 2>&1; then
182 echo "Something is missing!";
183 displayMissing;
184fi;
d7ffba91
SBS
185sleep 1;
186myFile=""; if ! checkfile "$myFile"; then echo "An empty argument provided:$myFile"; displayMissing; fi;
61b9303c 187#==END sample code==
65567c48 188
61b9303c
SBS
189# Author: Steven Baltaktei Sandoval
190# License: GPLv3+