2 # Desc: Checks that apps, files, or dirs exist. 
   4 #==BEGIN Define script parameters== 
   5 #===BEGIN Define variables=== 
   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 
   9 #===END Define variables=== 
  11 #===BEGIN Declare local script functions=== 
  13     # Desc: If arg is a command, save result in assoc array 'appRollCall' 
  14     # Usage: checkapp arg1 arg2 arg3 ... 
  16     # Input: global assoc. array 'appRollCall' 
  17     # Output: adds/updates key(value) to global assoc array 'appRollCall' 
  23         if command -v "$arg" 1>/dev
/null 
2>&1; then # Check if arg is a valid command 
  24             appRollCall
[$arg]="true"; 
  25             if ! [ "$returnState" = "false" ]; then returnState
="true"; fi; 
  27             appRollCall
[$arg]="false"; returnState
="false"; 
  31     #===Determine function return code=== 
  32     if [ "$returnState" = "true" ]; then 
  37 } # Check that app exists 
  39     # Desc: If arg is a file path, save result in assoc array 'fileRollCall' 
  40     # Usage: checkfile arg1 arg2 arg3 ... 
  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 
  50         if [ -f "$arg" ]; then 
  51             fileRollCall
["$arg"]="true"; 
  52             if ! [ "$returnState" = "false" ]; then returnState
="true"; fi; 
  54             fileRollCall
["$arg"]="false"; returnState
="false"; 
  58     #===Determine function return code=== 
  59     if [ "$returnState" = "true" ]; then 
  64 } # Check that file exists 
  66     # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' 
  67     # Usage: checkdir arg1 arg2 arg3 ... 
  69     # Input: global assoc. array 'dirRollCall' 
  70     # Output: adds/updates key(value) to global assoc array 'dirRollCall'; 
  71     # Output: returns 0 if all args are dirs; 1 otherwise 
  77         if [ -z "$arg" ]; then 
  78             dirRollCall
["(Unspecified Dirname(s))"]="false"; returnState
="false"; 
  79         elif [ -d "$arg" ]; then 
  80             dirRollCall
["$arg"]="true"; 
  81             if ! [ "$returnState" = "false" ]; then returnState
="true"; fi 
  83             dirRollCall
["$arg"]="false"; returnState
="false"; 
  87     #===Determine function return code=== 
  88     if [ "$returnState" = "true" ]; then 
  93 } # Check that dir exists 
  95     # Desc: Displays missing apps, files, and dirs 
  96     # Usage: displayMissing 
  98     # Input: associative arrays: appRollCall, fileRollCall, dirRollCall 
  99     # Output: stderr: messages indicating missing apps, file, or dirs 
 100     # Output: returns exit code 0 if nothing missing; 1 otherwise 
 101     # Depends: bash 5, checkAppFileDir() 
 102     local missingApps value appMissing missingFiles fileMissing
 
 103     local missingDirs dirMissing
 
 105     #==BEGIN Display errors== 
 106     #===BEGIN Display Missing Apps=== 
 107     missingApps
="Missing apps  :"; 
 108     #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done 
 109     for key 
in "${!appRollCall[@]}"; do 
 110         value
="${appRollCall[$key]}"; 
 111         if [ "$value" = "false" ]; then 
 112             #echo "DEBUG:Missing apps: $key => $value"; 
 113             missingApps
="$missingApps""$key "; 
 117     if [ "$appMissing" = "true" ]; then  # Only indicate if an app is missing. 
 118         echo "$missingApps" 1>&2; 
 121     #===END Display Missing Apps=== 
 123     #===BEGIN Display Missing Files=== 
 124     missingFiles
="Missing files:"; 
 125     #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done 
 126     for key 
in "${!fileRollCall[@]}"; do 
 127         value
="${fileRollCall[$key]}"; 
 128         if [ "$value" = "false" ]; then 
 129             #echo "DEBUG:Missing files: $key => $value"; 
 130             missingFiles
="$missingFiles""$key "; 
 134     if [ "$fileMissing" = "true" ]; then  # Only indicate if an app is missing. 
 135         echo "$missingFiles" 1>&2; 
 138     #===END Display Missing Files=== 
 140     #===BEGIN Display Missing Directories=== 
 141     missingDirs
="Missing dirs:"; 
 142     #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done 
 143     for key 
in "${!dirRollCall[@]}"; do 
 144         value
="${dirRollCall[$key]}"; 
 145         if [ "$value" = "false" ]; then 
 146             #echo "DEBUG:Missing dirs: $key => $value"; 
 147             missingDirs
="$missingDirs""$key "; 
 151     if [ "$dirMissing" = "true" ]; then  # Only indicate if an dir is missing. 
 152         echo "$missingDirs" 1>&2; 
 155     #===END Display Missing Directories=== 
 157     #==END Display errors== 
 158     #==BEGIN Determine function return code=== 
 159     if [ "$appMissing" == "true" ] || 
[ "$fileMissing" == "true" ] || 
[ "$dirMissing" == "true" ]; then 
 164     #==END Determine function return code=== 
 165 } # Display missing apps, files, dirs 
 167 #===END Declare local script functions=== 
 168 #==END Define script parameters== 
 170 #==BEGIN sample code== 
 171 if checkapp 
cat; then echo "cat found."; fi; 
 172 if checkapp gpg
; then echo "gpg found."; fi; 
 173 if displayMissing 
1>/dev
/null 
2>&1; then 
 174     echo "Nothing missing so far..."; 
 177 if checkapp emaaaacs
; then echo "emaaaacs found."; fi; 
 179 if ! displayMissing 
1>/dev
/null 
2>&1; then 
 180     echo "Something is missing!"; 
 185 # Author: Steven Baltaktei Sandoval