feat(user/bkots):Optimize with GNU Parallel for executing commands
[BK-2020-03.git] / unitproc / bktemp-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 ...
3d2d59ee 41 # Version: 0.1.1
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;
65567c48
SBS
53 else
54 fileRollCall["$arg"]="false"; returnState="false";
3d2d59ee
SBS
55 fi;
56 done;
65567c48
SBS
57
58 #===Determine function return code===
59 if [ "$returnState" = "true" ]; then
65567c48
SBS
60 return 0;
61 else
65567c48 62 return 1;
3d2d59ee 63 fi;
65567c48
SBS
64} # Check that file exists
65checkdir() {
66 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
67 # Usage: checkdir arg1 arg2 arg3 ...
b700f6ff 68 # Version 0.1.2
65567c48
SBS
69 # Input: global assoc. array 'dirRollCall'
70 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
b700f6ff 71 # Output: returns 0 if all args are dirs; 1 otherwise
3d2d59ee 72 # Depends: Bash 5.0.3
65567c48
SBS
73 local returnState
74
75 #===Process Args===
76 for arg in "$@"; do
b700f6ff
SBS
77 if [ -z "$arg" ]; then
78 dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false";
79 elif [ -d "$arg" ]; then
65567c48 80 dirRollCall["$arg"]="true";
65567c48
SBS
81 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
82 else
83 dirRollCall["$arg"]="false"; returnState="false";
84 fi
85 done
65567c48
SBS
86
87 #===Determine function return code===
88 if [ "$returnState" = "true" ]; then
65567c48
SBS
89 return 0;
90 else
65567c48
SBS
91 return 1;
92 fi
93} # Check that dir exists
61b9303c
SBS
94displayMissing() {
95 # Desc: Displays missing apps, files, and dirs
96 # Usage: displayMissing
b8f798aa 97 # Version 1.0.0
61b9303c
SBS
98 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
99 # Output: stderr: messages indicating missing apps, file, or dirs
b8f798aa 100 # Output: returns exit code 0 if nothing missing; 1 otherwise
61b9303c 101 # Depends: bash 5, checkAppFileDir()
3d2d59ee
SBS
102 local missingApps value appMissing missingFiles fileMissing
103 local missingDirs dirMissing
b8f798aa 104
61b9303c
SBS
105 #==BEGIN Display errors==
106 #===BEGIN Display Missing Apps===
3d2d59ee 107 missingApps="Missing apps :";
61b9303c
SBS
108 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
109 for key in "${!appRollCall[@]}"; do
3d2d59ee 110 value="${appRollCall[$key]}";
61b9303c
SBS
111 if [ "$value" = "false" ]; then
112 #echo "DEBUG:Missing apps: $key => $value";
3d2d59ee
SBS
113 missingApps="$missingApps""$key ";
114 appMissing="true";
115 fi;
116 done;
61b9303c
SBS
117 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
118 echo "$missingApps" 1>&2;
3d2d59ee
SBS
119 fi;
120 unset value;
61b9303c 121 #===END Display Missing Apps===
65567c48 122
61b9303c 123 #===BEGIN Display Missing Files===
3d2d59ee 124 missingFiles="Missing files:";
61b9303c
SBS
125 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
126 for key in "${!fileRollCall[@]}"; do
3d2d59ee 127 value="${fileRollCall[$key]}";
61b9303c
SBS
128 if [ "$value" = "false" ]; then
129 #echo "DEBUG:Missing files: $key => $value";
3d2d59ee
SBS
130 missingFiles="$missingFiles""$key ";
131 fileMissing="true";
132 fi;
133 done;
61b9303c
SBS
134 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
135 echo "$missingFiles" 1>&2;
3d2d59ee
SBS
136 fi;
137 unset value;
61b9303c 138 #===END Display Missing Files===
65567c48 139
61b9303c 140 #===BEGIN Display Missing Directories===
3d2d59ee 141 missingDirs="Missing dirs:";
61b9303c
SBS
142 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
143 for key in "${!dirRollCall[@]}"; do
3d2d59ee 144 value="${dirRollCall[$key]}";
61b9303c
SBS
145 if [ "$value" = "false" ]; then
146 #echo "DEBUG:Missing dirs: $key => $value";
3d2d59ee
SBS
147 missingDirs="$missingDirs""$key ";
148 dirMissing="true";
149 fi;
150 done;
61b9303c
SBS
151 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
152 echo "$missingDirs" 1>&2;
3d2d59ee
SBS
153 fi;
154 unset value;
61b9303c
SBS
155 #===END Display Missing Directories===
156
157 #==END Display errors==
b8f798aa
SBS
158 #==BEGIN Determine function return code===
159 if [ "$appMissing" == "true" ] || [ "$fileMissing" == "true" ] || [ "$dirMissing" == "true" ]; then
160 return 1;
161 else
162 return 0;
163 fi
164 #==END Determine function return code===
61b9303c
SBS
165} # Display missing apps, files, dirs
166
167#===END Declare local script functions===
168#==END Define script parameters==
65567c48 169
61b9303c
SBS
170#==BEGIN sample code==
171if checkapp cat; then echo "cat found."; fi;
172if checkapp gpg; then echo "gpg found."; fi;
b8f798aa
SBS
173if displayMissing 1>/dev/null 2>&1; then
174 echo "Nothing missing so far...";
175fi;
176
61b9303c
SBS
177if checkapp emaaaacs; then echo "emaaaacs found."; fi;
178sleep 1;
b8f798aa
SBS
179if ! displayMissing 1>/dev/null 2>&1; then
180 echo "Something is missing!";
181 displayMissing;
182fi;
61b9303c 183#==END sample code==
65567c48 184
61b9303c
SBS
185# Author: Steven Baltaktei Sandoval
186# License: GPLv3+