patternbashMinor
Bash script that checks if font is installed and installs it if necessary
Viewed 0 times
scriptnecessarychecksinstallsthatfontandinstalledbash
Problem
This checks if a font is installed by using
Concerns
I realize that I should not place semi-colons after each line as per the advice on my first question first question. I forgot to make the appropriate changes to this code snippet.
```
FONT_INSTALLED=$(fc-list | grep -i "roboto");
if [ -z "$FONT_INSTALLED" ]; then
echo "The Roboto font is not currently installed, would you like to install it now? (Y/N)";
read response
if [[ "$response" == [yY] ]]; then
echo "Installing the Roboto font to the ~/.fonts directory...";
mkdir .fonts/Roboto/ -p && wget --quiet -O ~/.fonts/Roboto/tmp.zip http://www.fontsquirrel.com/fonts/download/roboto &&
unzip ~/.fonts/Roboto/tmp.zip -d ~/.fonts/Roboto/ > /dev/null && rm ~/.fonts/Roboto/tmp.zip
fc-cache -fv > /dev/null
FONT_INSTALLED=$(fc-list | grep -i "roboto");
if [ -n "$FONT_INSTALLED" ]; then
echo "The Roboto font was sucessfully installed!";
else
echo "
fc-list and grep. If the particular font is not installed, it offers to install it for the user. This snippet is part of a larger script that installs a particular Conky configuration along with all of its dependencies with minimal effort from the user.Concerns
- I have to create the
FONT_INSTALLEDvariable to avoid a "too many arguments" error by placing the code into theifstatement, is there a better practice for handling this?
- To check if the font is installed, I check if the
FONT_INSTALLEDvariable is a nullStringor not. Is this considered good practice? I know in languages such as Java aStringis normally checked for length/content since the default value is "".
- I've seen that placing
> /dev/nullafter a command that would normally have output hides the output. This seems slightly 'hacky' as I know commands such aswgethave the-qor--quietoption to hide output. For commands that do not have such an option, what is the preferred method of hiding output?
I realize that I should not place semi-colons after each line as per the advice on my first question first question. I forgot to make the appropriate changes to this code snippet.
```
FONT_INSTALLED=$(fc-list | grep -i "roboto");
if [ -z "$FONT_INSTALLED" ]; then
echo "The Roboto font is not currently installed, would you like to install it now? (Y/N)";
read response
if [[ "$response" == [yY] ]]; then
echo "Installing the Roboto font to the ~/.fonts directory...";
mkdir .fonts/Roboto/ -p && wget --quiet -O ~/.fonts/Roboto/tmp.zip http://www.fontsquirrel.com/fonts/download/roboto &&
unzip ~/.fonts/Roboto/tmp.zip -d ~/.fonts/Roboto/ > /dev/null && rm ~/.fonts/Roboto/tmp.zip
fc-cache -fv > /dev/null
FONT_INSTALLED=$(fc-list | grep -i "roboto");
if [ -n "$FONT_INSTALLED" ]; then
echo "The Roboto font was sucessfully installed!";
else
echo "
Solution
If you are using bash then rather than mixing
You might also want to consider preferring
[ and [[, you should stick with one or the other, preferrably [[. See What is the difference between test, [ and [[ ?You might also want to consider preferring
printf over echo for a more robust approach. See this amazingly detailed analysis of why printf is better than echo.Context
StackExchange Code Review Q#88516, answer score: 4
Revisions (0)
No revisions yet.