| 1 | #!/bin/bash |
| 2 | # Desc: A list of executables to be started every day at midnight. |
| 3 | # Usage: nfg_run_daily_jobs.sh arg1 |
| 4 | # Input: arg1: path to directory containing ninfacyzga-1 repository |
| 5 | # arg2: |
| 6 | # Note: These jobs permit the general strategy of storing current instances of |
| 7 | # observed data in a temporary directory that can be accessed as a file from |
| 8 | # other scripts without each script having to know about eachother's |
| 9 | # existence. |
| 10 | # Note: Run this script daily at midnight using `cron`. |
| 11 | |
| 12 | #==BEGIN Define script parameters== |
| 13 | nfg_base_dir="../"; |
| 14 | nfg_exec_dir="$nfg_base_dir"/exec; |
| 15 | length_day_s=$((24 * 60 * 60)); # seconds |
| 16 | |
| 17 | #===BEGIN Define variables=== |
| 18 | declare -Ag appRollCall # Associative array for storing app status |
| 19 | declare -Ag fileRollCall # Associative array for storing file status |
| 20 | declare -Ag dirRollCall # Associative array for storing dir status |
| 21 | #===END Define variables=== |
| 22 | #===BEGIN Declare local script functions=== |
| 23 | yell() { echo "$0: $*" >&2; } # print script path and all args to stderr |
| 24 | die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status |
| 25 | try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails |
| 26 | checkapp() { |
| 27 | # Desc: If arg is a command, save result in assoc array 'appRollCall' |
| 28 | # Usage: checkapp arg1 arg2 arg3 ... |
| 29 | # Version: 0.1.1 |
| 30 | # Input: global assoc. array 'appRollCall' |
| 31 | # Output: adds/updates key(value) to global assoc array 'appRollCall' |
| 32 | # Depends: bash 5.0.3 |
| 33 | local returnState |
| 34 | |
| 35 | #===Process Args=== |
| 36 | for arg in "$@"; do |
| 37 | if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command |
| 38 | appRollCall[$arg]="true"; |
| 39 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi; |
| 40 | else |
| 41 | appRollCall[$arg]="false"; returnState="false"; |
| 42 | fi; |
| 43 | done; |
| 44 | |
| 45 | #===Determine function return code=== |
| 46 | if [ "$returnState" = "true" ]; then |
| 47 | return 0; |
| 48 | else |
| 49 | return 1; |
| 50 | fi; |
| 51 | } # Check that app exists |
| 52 | checkfile() { |
| 53 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' |
| 54 | # Usage: checkfile arg1 arg2 arg3 ... |
| 55 | # Version: 0.1.1 |
| 56 | # Input: global assoc. array 'fileRollCall' |
| 57 | # Output: adds/updates key(value) to global assoc array 'fileRollCall'; |
| 58 | # Output: returns 0 if app found, 1 otherwise |
| 59 | # Depends: bash 5.0.3 |
| 60 | local returnState |
| 61 | |
| 62 | #===Process Args=== |
| 63 | for arg in "$@"; do |
| 64 | if [ -f "$arg" ]; then |
| 65 | fileRollCall["$arg"]="true"; |
| 66 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi; |
| 67 | else |
| 68 | fileRollCall["$arg"]="false"; returnState="false"; |
| 69 | fi; |
| 70 | done; |
| 71 | |
| 72 | #===Determine function return code=== |
| 73 | if [ "$returnState" = "true" ]; then |
| 74 | return 0; |
| 75 | else |
| 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 ... |
| 82 | # Version 0.1.1 |
| 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 | # Depends: Bash 5.0.3 |
| 87 | local returnState |
| 88 | |
| 89 | #===Process Args=== |
| 90 | for arg in "$@"; do |
| 91 | if [ -d "$arg" ]; then |
| 92 | dirRollCall["$arg"]="true"; |
| 93 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi |
| 94 | else |
| 95 | dirRollCall["$arg"]="false"; returnState="false"; |
| 96 | fi |
| 97 | done |
| 98 | |
| 99 | #===Determine function return code=== |
| 100 | if [ "$returnState" = "true" ]; then |
| 101 | return 0; |
| 102 | else |
| 103 | return 1; |
| 104 | fi |
| 105 | } # Check that dir exists |
| 106 | displayMissing() { |
| 107 | # Desc: Displays missing apps, files, and dirs |
| 108 | # Usage: displayMissing |
| 109 | # Version 0.1.1 |
| 110 | # Input: associative arrays: appRollCall, fileRollCall, dirRollCall |
| 111 | # Output: stderr: messages indicating missing apps, file, or dirs |
| 112 | # Depends: bash 5, checkAppFileDir() |
| 113 | local missingApps value appMissing missingFiles fileMissing |
| 114 | local missingDirs dirMissing |
| 115 | |
| 116 | #==BEGIN Display errors== |
| 117 | #===BEGIN Display Missing Apps=== |
| 118 | missingApps="Missing apps :"; |
| 119 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done |
| 120 | for key in "${!appRollCall[@]}"; do |
| 121 | value="${appRollCall[$key]}"; |
| 122 | if [ "$value" = "false" ]; then |
| 123 | #echo "DEBUG:Missing apps: $key => $value"; |
| 124 | missingApps="$missingApps""$key "; |
| 125 | appMissing="true"; |
| 126 | fi; |
| 127 | done; |
| 128 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. |
| 129 | echo "$missingApps" 1>&2; |
| 130 | fi; |
| 131 | unset value; |
| 132 | #===END Display Missing Apps=== |
| 133 | |
| 134 | #===BEGIN Display Missing Files=== |
| 135 | missingFiles="Missing files:"; |
| 136 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done |
| 137 | for key in "${!fileRollCall[@]}"; do |
| 138 | value="${fileRollCall[$key]}"; |
| 139 | if [ "$value" = "false" ]; then |
| 140 | #echo "DEBUG:Missing files: $key => $value"; |
| 141 | missingFiles="$missingFiles""$key "; |
| 142 | fileMissing="true"; |
| 143 | fi; |
| 144 | done; |
| 145 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. |
| 146 | echo "$missingFiles" 1>&2; |
| 147 | fi; |
| 148 | unset value; |
| 149 | #===END Display Missing Files=== |
| 150 | |
| 151 | #===BEGIN Display Missing Directories=== |
| 152 | missingDirs="Missing dirs:"; |
| 153 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done |
| 154 | for key in "${!dirRollCall[@]}"; do |
| 155 | value="${dirRollCall[$key]}"; |
| 156 | if [ "$value" = "false" ]; then |
| 157 | #echo "DEBUG:Missing dirs: $key => $value"; |
| 158 | missingDirs="$missingDirs""$key "; |
| 159 | dirMissing="true"; |
| 160 | fi; |
| 161 | done; |
| 162 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. |
| 163 | echo "$missingDirs" 1>&2; |
| 164 | fi; |
| 165 | unset value; |
| 166 | #===END Display Missing Directories=== |
| 167 | |
| 168 | #==END Display errors== |
| 169 | } # Display missing apps, files, dirs |
| 170 | main() { |
| 171 | # Note: Use '&' to run scripts and immediately detach them. |
| 172 | |
| 173 | # Temperature |
| 174 | |
| 175 | # Pressure |
| 176 | |
| 177 | # Location |
| 178 | |
| 179 | # Photograph |
| 180 | photo_script_path="$nfg_exec_dir"/update_temp_photograph.sh; |
| 181 | if checkfile "$photo_script_path"; then |
| 182 | timeout "$length_day_s"s /bin/bash "$photo_script_path" & |
| 183 | else |
| 184 | yell "ERROR:Not found:$photo_script_path"; |
| 185 | fi; |
| 186 | displayMissing; |
| 187 | } # main program |
| 188 | |
| 189 | #===END Declare local script functions=== |
| 190 | #==END Define script parameters== |
| 191 | |
| 192 | main "$@"; |