0

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!

  • You are showing a work in progress without explaining what is correct or not. You need to be more specific in order for us to know what you want to solve. For instance there are clear placeholders (like `if not found`) but we don’t know if that is relevant for your issue. I also think that you are expecting some output or result, but you need to tell us what is your expected output and what is your current output. – vdavid Mar 12 '20 at 17:49
  • You can generally use `fc-list : family style file | grep -q fontname` to test if your font is installed. You may need to tailor your pattern used with `grep` or the format used with `fc-list`. The man page is very limited, but you can do a web search and find more detailed usage examples. For example for all true-type and open-type fonts in a filename fontname list you can use `fc-list -f "%-62{file} %{family} - %{style[0]}\n" | grep "otf\|ttf" | sort | sed 's|^.*/||'` – David C. Rankin Mar 12 '20 at 17:57
  • @DavidC.Rankin I appreciate the tip with fc-list, but I have had success with the command I am using above. I was not specific enough in my original post. I am most concerned with the nested conditional. If $i from ${fontArray} is not found, add that $i to ${missingFonts} so I can echo a list of fonts that are absent. If this is not the best way logically to take a list of fonts and verify them one by one sorting the failures to a new array, then I am way open to better suggestions. – ronin.andrew Mar 12 '20 at 18:58
  • @vdavid My apologies for not being more specific. I need to take a list of fonts and verify them one by one, sorting the failures to a new array so that I can display that list later along with other information. If $i from ${fontArray} is not found, add that $i to ${missingFonts} so I can echo a list of fonts that are absent. – ronin.andrew Mar 12 '20 at 19:00
  • @ronin.andrew You are re-explaining your algorithm. I am sorry to say that this does not help. What we need is to know what works (and what does not work) in the sample program you have written. Are you asking about whether you have the right algorithm, or about how to fill the gaps, e.g. how to write the `if not found` and the `add item to ${missingFonts}` ? – vdavid Mar 12 '20 at 19:06
  • 1
    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. – David C. Rankin Mar 12 '20 at 19:29
  • @DavidC.Rankin Your suggestion gets me a lot closer! However I am only seeing 1 time added to ${missingFonts} where I would expect more. Am I missing something that will run the system_profiler command agains each item in ${fontArray}? I am updating the original post to reflect current code – ronin.andrew Mar 12 '20 at 20:22
  • `for i in "${fontArray[@]}"; do` – David C. Rankin Mar 12 '20 at 20:27

1 Answers1

2

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

Dharman
  • 30,962
  • 25
  • 85
  • 135