Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Last active February 17, 2024 10:12
Show Gist options
  • Save JamieMason/4761049 to your computer and use it in GitHub Desktop.
Save JamieMason/4761049 to your computer and use it in GitHub Desktop.
Check if a program exists from a bash script.Thanks to twitter.com/joshnesbitt and twitter.com/mheap for the help with detecting npm packages.
#!/bin/bash
# Functions ==============================================
# return 1 if global command line program installed, else 0
# example
# echo "node: $(program_is_installed node)"
function program_is_installed {
# set to 1 initially
local return_=1
# set to 0 if not found
type $1 >/dev/null 2>&1 || { local return_=0; }
# return value
echo "$return_"
}
# return 1 if local npm package is installed at ./node_modules, else 0
# example
# echo "gruntacular : $(npm_package_is_installed gruntacular)"
function npm_package_is_installed {
# set to 1 initially
local return_=1
# set to 0 if not found
ls node_modules | grep $1 >/dev/null 2>&1 || { local return_=0; }
# return value
echo "$return_"
}
# display a message in red with a cross by it
# example
# echo echo_fail "No"
function echo_fail {
# echo first argument in red
printf "\e[31m✘ ${1}"
# reset colours back to normal
printf "\033\e[0m"
}
# display a message in green with a tick by it
# example
# echo echo_fail "Yes"
function echo_pass {
# echo first argument in green
printf "\e[32m✔ ${1}"
# reset colours back to normal
printf "\033\e[0m"
}
# echo pass or fail
# example
# echo echo_if 1 "Passed"
# echo echo_if 0 "Failed"
function echo_if {
if [ $1 == 1 ]; then
echo_pass $2
else
echo_fail $2
fi
}
# ============================================== Functions
# command line programs
echo "node $(echo_if $(program_is_installed node))"
echo "gulp $(echo_if $(program_is_installed gulp))"
echo "webpack $(echo_if $(program_is_installed webpack))"
echo "eslint $(echo_if $(program_is_installed eslint))"
echo "tsc $(echo_if $(program_is_installed tsc))"
echo "brew $(echo_if $(program_is_installed brew))"
echo "gem $(echo_if $(program_is_installed gem))"
# local npm packages
echo "lodash $(echo_if $(npm_package_is_installed lodash))"
echo "react $(echo_if $(npm_package_is_installed react))"
echo "angular $(echo_if $(npm_package_is_installed angular))"
@JamieMason
Copy link
Author

Updated, added coloured output and ticks/crosses

@JamieMason
Copy link
Author

Updated, fixed typo in echo_if's if statement

@edgardleal
Copy link

Great code. Thanks.

Note: in some system, need use { echo -e "\033[0m" } to reset colours.

@Stratus3D
Copy link

@edgardleal Thanks! On my system colors were not being reset. The -e flag fixed the issue.

@ivan-kleshnin
Copy link

npm_package_is_installed contains serious flaw
grep $1 should be grep -w $1 to match whole words only...

@FliiFe
Copy link

FliiFe commented Jul 14, 2016

Is this licensed ?

@MostHated
Copy link

MostHated commented Sep 9, 2016

Hello, when I run this I just get "nodejs ? \033[0m" and then everything just stays green no matter what.

**Edit - made a few adjustments, but still I only now get a green question mark no matter what the npm package I try and use.

http://i.imgur.com/AkZY2GF.png

http://i.imgur.com/weCQ4tx.png

The first one was with nodejs actually installed and checking for that, I change the name to nodej2s because it was not a real thing and got the same outcome.

@JamieMason
Copy link
Author

@FliiFe feel free to use this without restriction.

@JSONRice
Copy link

Love this!

@jpaulin
Copy link

jpaulin commented Mar 20, 2018

@MostHated Change 2 lines in the script.
Both occurrencies (there's two of them) of these:
echo "\033[0m"
=>
printf "\033\e[0m"

@JamieMason
Copy link
Author

thanks @jpaulin, I'll update

@hoshang82
Copy link

hoshang82 commented Apr 9, 2018

great work, thank you so much :-)do you know maybe whe I get the "sign" beside the green/red sign ?
printf "\e[32m✔ ${1}" , ${1} <- what does this variable ?
the sign is comming from reset to normal colours: printf "\033\e[0m"

Ubuntu 16.04

why

@frederickjh
Copy link

@hoshang82 I got this too. I Added this function and swapped the color and color reset codes to use the color variables provided by the fuction and it now works.

function loadcolor(){
# Colors  http://wiki.bash-hackers.org/snipplets/add_color_to_your_scripts
# More info about colors in bash http://misc.flogisoft.com/bash/tip_colors_and_formatting
esc_seq="\x1b["  #In Bash, the <Esc> character can be obtained with the following syntaxes:  \e  \033  \x1B
NC=$esc_seq"39;49;00m" # NC = Normal Color
red=$esc_seq"31;01m"
green=$esc_seq"32;00m"
yellow=$esc_seq"33;01m"
blue=$esc_seq"34;01m"
magenta=$esc_seq"35;01m"
cyan=$esc_seq"36;01m"
}

@muratgozel
Copy link

muratgozel commented Feb 5, 2019

I have modified the function a bit to use inside if conditions efficiently:

function program_doesnt_exist {
  local return_=1
  type $1 >/dev/null 2>&1 || { local return_=0; }
  # return $return_ instead of printing
  return $return_
}

# install nodejs
if program_doesnt_exist node; then
  curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
  sudo apt install -y nodejs
fi

# install certbot
if program_doesnt_exist certbot; then
  sudo add-apt-repository ppa:certbot/certbot -y
  sudo apt update
  sudo apt install -y python-certbot-nginx
fi

@flaxel
Copy link

flaxel commented May 30, 2020

Maybe you can write the functions a little more compact. For example, the code snippet for program_is_installed:

program_is_installed(){
	type $1 >/dev/null 2>&1 && echo "1" || echo "0"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment