Commit | Line | Data |
---|---|---|
65567c48 | 1 | #!/bin/bash |
9367c58e | 2 | # Desc: Template to check that apps, files, or dirs exist. |
9367c58e | 3 | |
65567c48 SBS |
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 | |
61b9303c | 8 | #==END Define script parameters== |
65567c48 SBS |
9 | |
10 | #===BEGIN Declare local script functions=== | |
11 | checkapp() { | |
12 | # Desc: If arg is a command, save result in assoc array 'appRollCall' | |
13 | # Usage: checkapp arg1 arg2 arg3 ... | |
61b9303c | 14 | # Version: 0.1.0 |
65567c48 SBS |
15 | # Input: global assoc. array 'appRollCall' |
16 | # Output: adds/updates key(value) to global assoc array 'appRollCall' | |
17 | local returnState | |
18 | #echo "DEBUG:$(date +%S.%N)..Starting checkapp function." | |
19 | #echo "DEBUG:args: $@" | |
20 | #echo "DEBUG:returnState:$returnState" | |
21 | ||
22 | #===Process Args=== | |
23 | for arg in "$@"; do | |
24 | #echo "DEBUG:processing arg:$arg" | |
0733880e | 25 | if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command |
65567c48 SBS |
26 | appRollCall[$arg]="true"; |
27 | #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]} | |
28 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
29 | else | |
30 | appRollCall[$arg]="false"; returnState="false"; | |
31 | fi | |
32 | done | |
33 | ||
34 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done | |
35 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
36 | ||
37 | #===Determine function return code=== | |
38 | if [ "$returnState" = "true" ]; then | |
39 | #echo "DEBUG:checkapp returns true for $arg"; | |
40 | return 0; | |
41 | else | |
42 | #echo "DEBUG:checkapp returns false for $arg"; | |
43 | return 1; | |
44 | fi | |
45 | } # Check that app exists | |
46 | checkfile() { | |
47 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' | |
48 | # Usage: checkfile arg1 arg2 arg3 ... | |
61b9303c | 49 | # Version: 0.1.0 |
65567c48 SBS |
50 | # Input: global assoc. array 'fileRollCall' |
51 | # Output: adds/updates key(value) to global assoc array 'fileRollCall'; | |
52 | # Output: returns 0 if app found, 1 otherwise | |
53 | local returnState | |
54 | ||
55 | #===Process Args=== | |
56 | for arg in "$@"; do | |
57 | #echo "DEBUG:processing arg:$arg" | |
58 | if [ -f "$arg" ]; then | |
59 | fileRollCall["$arg"]="true"; | |
60 | #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]} | |
61 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
62 | else | |
63 | fileRollCall["$arg"]="false"; returnState="false"; | |
64 | fi | |
65 | done | |
66 | ||
67 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done | |
68 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
69 | ||
70 | #===Determine function return code=== | |
71 | if [ "$returnState" = "true" ]; then | |
72 | #echo "DEBUG:checkapp returns true for $arg"; | |
73 | return 0; | |
74 | else | |
75 | #echo "DEBUG:checkapp returns false for $arg"; | |
76 | return 1; | |
77 | fi | |
78 | } # Check that file exists | |
79 | checkdir() { | |
80 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' | |
81 | # Usage: checkdir arg1 arg2 arg3 ... | |
61b9303c | 82 | # Version 0.1.0 |
65567c48 SBS |
83 | # Input: global assoc. array 'dirRollCall' |
84 | # Output: adds/updates key(value) to global assoc array 'dirRollCall'; | |
85 | # Output: returns 0 if app found, 1 otherwise | |
86 | local returnState | |
87 | ||
88 | #===Process Args=== | |
89 | for arg in "$@"; do | |
90 | #echo "DEBUG:processing arg:$arg" | |
91 | if [ -d "$arg" ]; then | |
92 | dirRollCall["$arg"]="true"; | |
93 | #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]} | |
94 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
95 | else | |
96 | dirRollCall["$arg"]="false"; returnState="false"; | |
97 | fi | |
98 | done | |
99 | ||
100 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done | |
101 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
102 | ||
103 | #===Determine function return code=== | |
104 | if [ "$returnState" = "true" ]; then | |
105 | #echo "DEBUG:checkapp returns true for $arg"; | |
106 | return 0; | |
107 | else | |
108 | #echo "DEBUG:checkapp returns false for $arg"; | |
109 | return 1; | |
110 | fi | |
111 | } # Check that dir exists | |
61b9303c SBS |
112 | displayMissing() { |
113 | # Desc: Displays missing apps, files, and dirs | |
114 | # Usage: displayMissing | |
115 | # Version 0.1.0 | |
116 | # Input: associative arrays: appRollCall, fileRollCall, dirRollCall | |
117 | # Output: stderr: messages indicating missing apps, file, or dirs | |
118 | # Depends: bash 5, checkAppFileDir() | |
119 | #==BEGIN Display errors== | |
120 | #===BEGIN Display Missing Apps=== | |
121 | missingApps="Missing apps :" | |
122 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done | |
123 | for key in "${!appRollCall[@]}"; do | |
124 | value="${appRollCall[$key]}" | |
125 | if [ "$value" = "false" ]; then | |
126 | #echo "DEBUG:Missing apps: $key => $value"; | |
127 | missingApps="$missingApps""$key " | |
128 | appMissing="true" | |
129 | fi | |
130 | done | |
131 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. | |
132 | echo "$missingApps" 1>&2; | |
65567c48 | 133 | fi |
61b9303c | 134 | #===END Display Missing Apps=== |
65567c48 | 135 | |
61b9303c SBS |
136 | #===BEGIN Display Missing Files=== |
137 | missingFiles="Missing files:" | |
138 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done | |
139 | for key in "${!fileRollCall[@]}"; do | |
140 | value="${fileRollCall[$key]}" | |
141 | if [ "$value" = "false" ]; then | |
142 | #echo "DEBUG:Missing files: $key => $value"; | |
143 | missingFiles="$missingFiles""$key " | |
144 | fileMissing="true" | |
145 | fi | |
146 | done | |
147 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. | |
148 | echo "$missingFiles" 1>&2; | |
65567c48 | 149 | fi |
61b9303c | 150 | #===END Display Missing Files=== |
65567c48 | 151 | |
61b9303c SBS |
152 | #===BEGIN Display Missing Directories=== |
153 | missingDirs="Missing dirs:" | |
154 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done | |
155 | for key in "${!dirRollCall[@]}"; do | |
156 | value="${dirRollCall[$key]}" | |
157 | if [ "$value" = "false" ]; then | |
158 | #echo "DEBUG:Missing dirs: $key => $value"; | |
159 | missingDirs="$missingDirs""$key " | |
160 | dirMissing="true" | |
161 | fi | |
162 | done | |
163 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. | |
164 | echo "$missingDirs" 1>&2; | |
65567c48 | 165 | fi |
61b9303c SBS |
166 | #===END Display Missing Directories=== |
167 | ||
168 | #==END Display errors== | |
169 | } # Display missing apps, files, dirs | |
170 | ||
171 | #===END Declare local script functions=== | |
172 | #==END Define script parameters== | |
65567c48 | 173 | |
61b9303c SBS |
174 | #==BEGIN sample code== |
175 | if checkapp cat; then echo "cat found."; fi; | |
176 | if checkapp gpg; then echo "gpg found."; fi; | |
177 | if checkapp emaaaacs; then echo "emaaaacs found."; fi; | |
178 | sleep 1; | |
179 | displayMissing; | |
180 | #==END sample code== | |
65567c48 | 181 | |
61b9303c SBS |
182 | # Author: Steven Baltaktei Sandoval |
183 | # License: GPLv3+ |