Merge branch 'master' of https://zdv2.bktei.com/gitweb/baltakatei-exdev into develop
[BK-2020-03.git] / unitproc / bktemp-checkAppFileDir
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Checks that apps, files, or dirs exist.
3
4#==BEGIN Define script parameters==
5declare -Ag appRollCall # Associative array for storing app status
6declare -Ag fileRollCall # Associative array for storing file status
7declare -Ag dirRollCall # Associative array for storing dir status
8#==END Define script parameters==
9
10#===BEGIN Declare local script functions===
11checkapp() {
12 # Desc: If arg is a command, save result in assoc array 'appRollCall'
13 # Usage: checkapp arg1 arg2 arg3 ...
14 # Version: 0.1.1
15 # Input: global assoc. array 'appRollCall'
16 # Output: adds/updates key(value) to global assoc array 'appRollCall'
17 # Depends: bash 5.0.3
18 local returnState
19
20 #===Process Args===
21 for arg in "$@"; do
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;
25 else
26 appRollCall[$arg]="false"; returnState="false";
27 fi;
28 done;
29
30 #===Determine function return code===
31 if [ "$returnState" = "true" ]; then
32 return 0;
33 else
34 return 1;
35 fi;
36} # Check that app exists
37checkfile() {
38 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
39 # Usage: checkfile arg1 arg2 arg3 ...
40 # Version: 0.1.1
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
44 # Depends: bash 5.0.3
45 local returnState
46
47 #===Process Args===
48 for arg in "$@"; do
49 if [ -f "$arg" ]; then
50 fileRollCall["$arg"]="true";
51 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
52 else
53 fileRollCall["$arg"]="false"; returnState="false";
54 fi;
55 done;
56
57 #===Determine function return code===
58 if [ "$returnState" = "true" ]; then
59 return 0;
60 else
61 return 1;
62 fi;
63} # Check that file exists
64checkdir() {
65 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
66 # Usage: checkdir arg1 arg2 arg3 ...
67 # Version 0.1.1
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
71 # Depends: Bash 5.0.3
72 local returnState
73
74 #===Process Args===
75 for arg in "$@"; do
76 if [ -d "$arg" ]; then
77 dirRollCall["$arg"]="true";
78 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
79 else
80 dirRollCall["$arg"]="false"; returnState="false";
81 fi
82 done
83
84 #===Determine function return code===
85 if [ "$returnState" = "true" ]; then
86 return 0;
87 else
88 return 1;
89 fi
90} # Check that dir exists
91displayMissing() {
92 # Desc: Displays missing apps, files, and dirs
93 # Usage: displayMissing
94 # Version 0.1.1
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
100
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 ";
110 appMissing="true";
111 fi;
112 done;
113 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
114 echo "$missingApps" 1>&2;
115 fi;
116 unset value;
117 #===END Display Missing Apps===
118
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 ";
127 fileMissing="true";
128 fi;
129 done;
130 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
131 echo "$missingFiles" 1>&2;
132 fi;
133 unset value;
134 #===END Display Missing Files===
135
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 ";
144 dirMissing="true";
145 fi;
146 done;
147 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
148 echo "$missingDirs" 1>&2;
149 fi;
150 unset value;
151 #===END Display Missing Directories===
152
153 #==END Display errors==
154} # Display missing apps, files, dirs
155
156#===END Declare local script functions===
157#==END Define script parameters==
158
159#==BEGIN sample code==
160if checkapp cat; then echo "cat found."; fi;
161if checkapp gpg; then echo "gpg found."; fi;
162if checkapp emaaaacs; then echo "emaaaacs found."; fi;
163sleep 1;
164displayMissing;
165#==END sample code==
166
167# Author: Steven Baltaktei Sandoval
168# License: GPLv3+