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 app found, 1 otherwise 
  77         if [ -d "$arg" ]; then 
  78             dirRollCall
["$arg"]="true"; 
  79             if ! [ "$returnState" = "false" ]; then returnState
="true"; fi 
  81             dirRollCall
["$arg"]="false"; returnState
="false"; 
  85     #===Determine function return code=== 
  86     if [ "$returnState" = "true" ]; then 
  91 } # Check that dir exists 
  93     # Desc: Displays missing apps, files, and dirs 
  94     # Usage: displayMissing 
  96     # Input: associative arrays: appRollCall, fileRollCall, dirRollCall 
  97     # Output: stderr: messages indicating missing apps, file, or dirs 
  98     # Depends: bash 5, checkAppFileDir() 
  99     local missingApps value appMissing missingFiles fileMissing
 
 100     local missingDirs dirMissing
 
 102     #==BEGIN Display errors== 
 103     #===BEGIN Display Missing Apps=== 
 104     missingApps
="Missing apps  :"; 
 105     #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done 
 106     for key 
in "${!appRollCall[@]}"; do 
 107         value
="${appRollCall[$key]}"; 
 108         if [ "$value" = "false" ]; then 
 109             #echo "DEBUG:Missing apps: $key => $value"; 
 110             missingApps
="$missingApps""$key "; 
 114     if [ "$appMissing" = "true" ]; then  # Only indicate if an app is missing. 
 115         echo "$missingApps" 1>&2; 
 118     #===END Display Missing Apps=== 
 120     #===BEGIN Display Missing Files=== 
 121     missingFiles
="Missing files:"; 
 122     #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done 
 123     for key 
in "${!fileRollCall[@]}"; do 
 124         value
="${fileRollCall[$key]}"; 
 125         if [ "$value" = "false" ]; then 
 126             #echo "DEBUG:Missing files: $key => $value"; 
 127             missingFiles
="$missingFiles""$key "; 
 131     if [ "$fileMissing" = "true" ]; then  # Only indicate if an app is missing. 
 132         echo "$missingFiles" 1>&2; 
 135     #===END Display Missing Files=== 
 137     #===BEGIN Display Missing Directories=== 
 138     missingDirs
="Missing dirs:"; 
 139     #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done 
 140     for key 
in "${!dirRollCall[@]}"; do 
 141         value
="${dirRollCall[$key]}"; 
 142         if [ "$value" = "false" ]; then 
 143             #echo "DEBUG:Missing dirs: $key => $value"; 
 144             missingDirs
="$missingDirs""$key "; 
 148     if [ "$dirMissing" = "true" ]; then  # Only indicate if an dir is missing. 
 149         echo "$missingDirs" 1>&2; 
 152     #===END Display Missing Directories=== 
 154     #==END Display errors== 
 155 } # Display missing apps, files, dirs 
 157 #===END Declare local script functions=== 
 158 #==END Define script parameters== 
 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; 
 168 # Author: Steven Baltaktei Sandoval