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