Commit | Line | Data |
---|---|---|
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 |
6 | declare -Ag appRollCall # Associative array for storing app status |
7 | declare -Ag fileRollCall # Associative array for storing file status | |
8 | declare -Ag dirRollCall # Associative array for storing dir status | |
2928a8e4 | 9 | #===END Define variables=== |
65567c48 SBS |
10 | |
11 | #===BEGIN Declare local script functions=== | |
12 | checkapp() { | |
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 |
38 | checkfile() { | |
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 |
65 | checkdir() { | |
66 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' | |
67 | # Usage: checkdir arg1 arg2 arg3 ... | |
3d2d59ee | 68 | # Version 0.1.1 |
65567c48 SBS |
69 | # Input: global assoc. array 'dirRollCall' |
70 | # Output: adds/updates key(value) to global assoc array 'dirRollCall'; | |
71 | # Output: returns 0 if app found, 1 otherwise | |
3d2d59ee | 72 | # Depends: Bash 5.0.3 |
65567c48 SBS |
73 | local returnState |
74 | ||
75 | #===Process Args=== | |
76 | for arg in "$@"; do | |
65567c48 SBS |
77 | if [ -d "$arg" ]; then |
78 | dirRollCall["$arg"]="true"; | |
65567c48 SBS |
79 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi |
80 | else | |
81 | dirRollCall["$arg"]="false"; returnState="false"; | |
82 | fi | |
83 | done | |
65567c48 SBS |
84 | |
85 | #===Determine function return code=== | |
86 | if [ "$returnState" = "true" ]; then | |
65567c48 SBS |
87 | return 0; |
88 | else | |
65567c48 SBS |
89 | return 1; |
90 | fi | |
91 | } # Check that dir exists | |
61b9303c SBS |
92 | displayMissing() { |
93 | # Desc: Displays missing apps, files, and dirs | |
94 | # Usage: displayMissing | |
3d2d59ee | 95 | # Version 0.1.1 |
61b9303c SBS |
96 | # Input: associative arrays: appRollCall, fileRollCall, dirRollCall |
97 | # Output: stderr: messages indicating missing apps, file, or dirs | |
98 | # Depends: bash 5, checkAppFileDir() | |
3d2d59ee SBS |
99 | local missingApps value appMissing missingFiles fileMissing |
100 | local missingDirs dirMissing | |
101 | ||
61b9303c SBS |
102 | #==BEGIN Display errors== |
103 | #===BEGIN Display Missing Apps=== | |
3d2d59ee | 104 | missingApps="Missing apps :"; |
61b9303c SBS |
105 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done |
106 | for key in "${!appRollCall[@]}"; do | |
3d2d59ee | 107 | value="${appRollCall[$key]}"; |
61b9303c SBS |
108 | if [ "$value" = "false" ]; then |
109 | #echo "DEBUG:Missing apps: $key => $value"; | |
3d2d59ee SBS |
110 | missingApps="$missingApps""$key "; |
111 | appMissing="true"; | |
112 | fi; | |
113 | done; | |
61b9303c SBS |
114 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. |
115 | echo "$missingApps" 1>&2; | |
3d2d59ee SBS |
116 | fi; |
117 | unset value; | |
61b9303c | 118 | #===END Display Missing Apps=== |
65567c48 | 119 | |
61b9303c | 120 | #===BEGIN Display Missing Files=== |
3d2d59ee | 121 | missingFiles="Missing files:"; |
61b9303c SBS |
122 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done |
123 | for key in "${!fileRollCall[@]}"; do | |
3d2d59ee | 124 | value="${fileRollCall[$key]}"; |
61b9303c SBS |
125 | if [ "$value" = "false" ]; then |
126 | #echo "DEBUG:Missing files: $key => $value"; | |
3d2d59ee SBS |
127 | missingFiles="$missingFiles""$key "; |
128 | fileMissing="true"; | |
129 | fi; | |
130 | done; | |
61b9303c SBS |
131 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. |
132 | echo "$missingFiles" 1>&2; | |
3d2d59ee SBS |
133 | fi; |
134 | unset value; | |
61b9303c | 135 | #===END Display Missing Files=== |
65567c48 | 136 | |
61b9303c | 137 | #===BEGIN Display Missing Directories=== |
3d2d59ee | 138 | missingDirs="Missing dirs:"; |
61b9303c SBS |
139 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done |
140 | for key in "${!dirRollCall[@]}"; do | |
3d2d59ee | 141 | value="${dirRollCall[$key]}"; |
61b9303c SBS |
142 | if [ "$value" = "false" ]; then |
143 | #echo "DEBUG:Missing dirs: $key => $value"; | |
3d2d59ee SBS |
144 | missingDirs="$missingDirs""$key "; |
145 | dirMissing="true"; | |
146 | fi; | |
147 | done; | |
61b9303c SBS |
148 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. |
149 | echo "$missingDirs" 1>&2; | |
3d2d59ee SBS |
150 | fi; |
151 | unset value; | |
61b9303c SBS |
152 | #===END Display Missing Directories=== |
153 | ||
154 | #==END Display errors== | |
155 | } # Display missing apps, files, dirs | |
156 | ||
157 | #===END Declare local script functions=== | |
158 | #==END Define script parameters== | |
65567c48 | 159 | |
61b9303c SBS |
160 | #==BEGIN sample code== |
161 | if checkapp cat; then echo "cat found."; fi; | |
162 | if checkapp gpg; then echo "gpg found."; fi; | |
163 | if checkapp emaaaacs; then echo "emaaaacs found."; fi; | |
164 | sleep 1; | |
165 | displayMissing; | |
166 | #==END sample code== | |
65567c48 | 167 | |
61b9303c SBS |
168 | # Author: Steven Baltaktei Sandoval |
169 | # License: GPLv3+ |