| 1 | #!/bin/bash |
| 2 | |
| 3 | #==BEGIN Define script parameters== |
| 4 | declare -Ag appRollCall # Associative array for storing app status |
| 5 | declare -Ag fileRollCall # Associative array for storing file status |
| 6 | declare -Ag dirRollCall # Associative array for storing dir status |
| 7 | |
| 8 | #===BEGIN Declare local script functions=== |
| 9 | checkapp() { |
| 10 | # Desc: If arg is a command, save result in assoc array 'appRollCall' |
| 11 | # Usage: checkapp arg1 arg2 arg3 ... |
| 12 | # Input: global assoc. array 'appRollCall' |
| 13 | # Output: adds/updates key(value) to global assoc array 'appRollCall' |
| 14 | local returnState |
| 15 | #echo "DEBUG:$(date +%S.%N)..Starting checkapp function." |
| 16 | #echo "DEBUG:args: $@" |
| 17 | #echo "DEBUG:returnState:$returnState" |
| 18 | |
| 19 | #===Process Args=== |
| 20 | for arg in "$@"; do |
| 21 | #echo "DEBUG:processing arg:$arg" |
| 22 | if command -v $arg 1>/dev/null 2>&1; then # Check if arg is a valid command |
| 23 | appRollCall[$arg]="true"; |
| 24 | #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]} |
| 25 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi |
| 26 | else |
| 27 | appRollCall[$arg]="false"; returnState="false"; |
| 28 | fi |
| 29 | done |
| 30 | |
| 31 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done |
| 32 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState |
| 33 | |
| 34 | #===Determine function return code=== |
| 35 | if [ "$returnState" = "true" ]; then |
| 36 | #echo "DEBUG:checkapp returns true for $arg"; |
| 37 | return 0; |
| 38 | else |
| 39 | #echo "DEBUG:checkapp returns false for $arg"; |
| 40 | return 1; |
| 41 | fi |
| 42 | } # Check that app exists |
| 43 | checkfile() { |
| 44 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' |
| 45 | # Usage: checkfile arg1 arg2 arg3 ... |
| 46 | # Input: global assoc. array 'fileRollCall' |
| 47 | # Output: adds/updates key(value) to global assoc array 'fileRollCall'; |
| 48 | # Output: returns 0 if app found, 1 otherwise |
| 49 | local returnState |
| 50 | |
| 51 | #===Process Args=== |
| 52 | for arg in "$@"; do |
| 53 | #echo "DEBUG:processing arg:$arg" |
| 54 | if [ -f "$arg" ]; then |
| 55 | fileRollCall["$arg"]="true"; |
| 56 | #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]} |
| 57 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi |
| 58 | else |
| 59 | fileRollCall["$arg"]="false"; returnState="false"; |
| 60 | fi |
| 61 | done |
| 62 | |
| 63 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done |
| 64 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState |
| 65 | |
| 66 | #===Determine function return code=== |
| 67 | if [ "$returnState" = "true" ]; then |
| 68 | #echo "DEBUG:checkapp returns true for $arg"; |
| 69 | return 0; |
| 70 | else |
| 71 | #echo "DEBUG:checkapp returns false for $arg"; |
| 72 | return 1; |
| 73 | fi |
| 74 | } # Check that file exists |
| 75 | checkdir() { |
| 76 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' |
| 77 | # Usage: checkdir arg1 arg2 arg3 ... |
| 78 | # Input: global assoc. array 'dirRollCall' |
| 79 | # Output: adds/updates key(value) to global assoc array 'dirRollCall'; |
| 80 | # Output: returns 0 if app found, 1 otherwise |
| 81 | local returnState |
| 82 | |
| 83 | #===Process Args=== |
| 84 | for arg in "$@"; do |
| 85 | #echo "DEBUG:processing arg:$arg" |
| 86 | if [ -d "$arg" ]; then |
| 87 | dirRollCall["$arg"]="true"; |
| 88 | #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]} |
| 89 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi |
| 90 | else |
| 91 | dirRollCall["$arg"]="false"; returnState="false"; |
| 92 | fi |
| 93 | done |
| 94 | |
| 95 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done |
| 96 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState |
| 97 | |
| 98 | #===Determine function return code=== |
| 99 | if [ "$returnState" = "true" ]; then |
| 100 | #echo "DEBUG:checkapp returns true for $arg"; |
| 101 | return 0; |
| 102 | else |
| 103 | #echo "DEBUG:checkapp returns false for $arg"; |
| 104 | return 1; |
| 105 | fi |
| 106 | } # Check that dir exists |
| 107 | #===END Declare local script functions=== |
| 108 | #==END Define script parameters== |
| 109 | |
| 110 | #==BEGIN sample code== |
| 111 | if checkapp cat; then echo "cat found."; fi |
| 112 | if checkapp gpg; then echo "gpg found."; fi |
| 113 | if checkapp emaaaacs; then echo "emaaaacs found."; fi |
| 114 | #==END sample code== |
| 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 | #===END Display Missing Apps=== |
| 132 | |
| 133 | #===BEGIN Display Missing Files=== |
| 134 | missingFiles="Missing files:" |
| 135 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done |
| 136 | for key in "${!fileRollCall[@]}"; do |
| 137 | value="${fileRollCall[$key]}" |
| 138 | if [ "$value" = "false" ]; then |
| 139 | #echo "DEBUG:Missing files: $key => $value"; |
| 140 | missingFiles="$missingFiles""$key " |
| 141 | fileMissing="true" |
| 142 | fi |
| 143 | done |
| 144 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. |
| 145 | echo "$missingFiles" 1>&2; |
| 146 | fi |
| 147 | #===END Display Missing Files=== |
| 148 | |
| 149 | #===BEGIN Display Missing Directories=== |
| 150 | missingDirs="Missing dirs:" |
| 151 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done |
| 152 | for key in "${!dirRollCall[@]}"; do |
| 153 | value="${dirRollCall[$key]}" |
| 154 | if [ "$value" = "false" ]; then |
| 155 | #echo "DEBUG:Missing dirs: $key => $value"; |
| 156 | missingDirs="$missingDirs""$key " |
| 157 | dirMissing="true" |
| 158 | fi |
| 159 | done |
| 160 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. |
| 161 | echo "$missingDirs" 1>&2; |
| 162 | fi |
| 163 | #===END Display Missing Directories=== |
| 164 | |
| 165 | #===BEGIN Display Failed Functions=== |
| 166 | if [ ${#failedFunctions} -gt 0 ]; then # Only indicate if an function failed. |
| 167 | echo "Failed functions:${failedFunctions[@]}" 1>&2; |
| 168 | fi |
| 169 | #===END Display Failed Functions=== |
| 170 | |
| 171 | #==END Display errors== |