Search code examples
arraysbashmacosshellsystem-profiler

bash script checking for installed fonts from list in an array and for loop to do commands


Updated with working code

I am trying to check for installed fonts listed in ${fontArray} one by one and add any that are not found to a new array ${missingFonts} that I can print later as part of a much longer "post-build health check" run on each machine in my environment.

    Var19="Fonts"
fontArray=("font1" "font2" "font3")
missingFonts=()
for i in "${fontArray[@]}"; do
    system_profiler SPFontsDataType | grep "Full Name: $i" | sed 's/.*: //'
    if ! system_profiler SPFontsDataType | grep -q "Full Name: $i"; then 
        missingFonts+=("$i");
    fi
done

if [ ${#missingFonts[@]} -eq 0 ]; then
    Val9="Fonts Installed"
    Check19=PASS
else
    Val19="Missing Fonts: ${missingFonts[@]}"
    Check19=FAIL
fi

Line19=" | ${Check19} | ${Var19}        = ${Val19} "

echo "$Line19"

exit 0

which returns

| FAIL | Fonts      = Missing Fonts: font1 font2 font3

Thanks in advance for helping an old dog learn new tricks!


Solution

  • Thanks @DavidC.Rankin for the assist!

    Why not then

    if ! system_profiler SPFontsDataType | grep -q "Full Name: $i"; 
    then missingFonts+=("$i"); 
    fi 
    

    to add the missing fonts? Don't worry about stripping the prefix with sed unless you need to. That should build your missing font list