fix(user/bkots):Exit code 0 if script finishes
[BK-2020-03.git] / sysutils / tick_minute.sh
CommitLineData
7b45d6a9
SBS
1#!/bin/bash
2
3# Desc: Outputs to stdout the current iso-8601 timestamp (minute accuracy)
4# Usage: tick_minute.sh
5# Example tick_minute.sh
6
7#==BEGIN Define script parameters==
8#===BEGIN Declare variables===
9declare -Ag appRollCall # Associative array for storing app status
10declare -Ag fileRollCall # Associative array for storing file status
11declare -Ag dirRollCall # Associative array for storing dir status
12declare exitFlag
13#===END Declare variables===
14
15
16#===BEGIN Declare local script functions===
17yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
18die() { yell "$*"; exit 111; }
19try() { "$@" || die "cannot $*"; }
20checkapp() {
21 # Desc: If arg is a command, save result in assoc array 'appRollCall'
22 # Usage: checkapp arg1 arg2 arg3 ...
23 # Version: 0.1.1
24 # Input: global assoc. array 'appRollCall'
25 # Output: adds/updates key(value) to global assoc array 'appRollCall'
26 # Depends: bash 5.0.3
27 local returnState
28
29 #===Process Args===
30 for arg in "$@"; do
31 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
32 appRollCall[$arg]="true";
33 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
34 else
35 appRollCall[$arg]="false"; returnState="false";
36 fi;
37 done;
38
39 #===Determine function return code===
40 if [ "$returnState" = "true" ]; then
41 return 0;
42 else
43 return 1;
44 fi;
45} # Check that app exists
46displayMissing() {
47 # Desc: Displays missing apps, files, and dirs
48 # Usage: displayMissing
49 # Version 0.1.1
50 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
51 # Output: stderr: messages indicating missing apps, file, or dirs
52 # Depends: bash 5, checkAppFileDir()
53 local missingApps value appMissing missingFiles fileMissing
54 local missingDirs dirMissing
55
56 #==BEGIN Display errors==
57 #===BEGIN Display Missing Apps===
58 missingApps="Missing apps :";
59 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
60 for key in "${!appRollCall[@]}"; do
61 value="${appRollCall[$key]}";
62 if [ "$value" = "false" ]; then
63 #echo "DEBUG:Missing apps: $key => $value";
64 missingApps="$missingApps""$key ";
65 appMissing="true";
66 fi;
67 done;
68 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
69 echo "$missingApps" 1>&2;
70 fi;
71 unset value;
72 #===END Display Missing Apps===
73
74 #===BEGIN Display Missing Files===
75 missingFiles="Missing files:";
76 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
77 for key in "${!fileRollCall[@]}"; do
78 value="${fileRollCall[$key]}";
79 if [ "$value" = "false" ]; then
80 #echo "DEBUG:Missing files: $key => $value";
81 missingFiles="$missingFiles""$key ";
82 fileMissing="true";
83 fi;
84 done;
85 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
86 echo "$missingFiles" 1>&2;
87 fi;
88 unset value;
89 #===END Display Missing Files===
90
91 #===BEGIN Display Missing Directories===
92 missingDirs="Missing dirs:";
93 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
94 for key in "${!dirRollCall[@]}"; do
95 value="${dirRollCall[$key]}";
96 if [ "$value" = "false" ]; then
97 #echo "DEBUG:Missing dirs: $key => $value";
98 missingDirs="$missingDirs""$key ";
99 dirMissing="true";
100 fi;
101 done;
102 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
103 echo "$missingDirs" 1>&2;
104 fi;
105 unset value;
106 #===END Display Missing Directories===
107
108 #==END Display errors==
109} # Display missing apps, files, dirsw
110main() {
111 # Desc: main program
112 # Input: var: arg1: path
113 # var: appRollCall checkapp() dependency
114 # var: exitFlag indicates to script if exit required
115 # Output: stdout: iso-8601 date string
116 # Depends: try()
117
118 # Validate input
119 ## Check argument count (must be 0)
120 if [[ $# -gt 0 ]]; then yell "ERROR:Too many arguments:$#"; exitFlag="true"; fi;
121
122 ## Check Apps
123 if ! checkapp date echo; then yell "ERROR:Missing App"; exitFlag="true"; fi;
124
125 ## Report errors
126 displayMissing;
127 if [[ $exitFlag == "true" ]]; then exit 1; fi;
128
129 # Form output
130 output="$(date +%Y-%m-%dT%H:%M%z)"; #iso-8601 string (e.g. 2021-05-28T19:18+0000)
131
132 # Write output
133 echo "$output";
134}
135
136#===END Declare local script functions===
137#==END Define script parameters==
138
139try main "$@";