Skip to content

Instantly share code, notes, and snippets.

@jamescostian
Created December 15, 2019 23:02
Show Gist options
  • Select an option

  • Save jamescostian/31ef46194d27da535487e1f43a35b02e to your computer and use it in GitHub Desktop.

Select an option

Save jamescostian/31ef46194d27da535487e1f43a35b02e to your computer and use it in GitHub Desktop.
Given a string of programs, try to install them
function install_if_not_exists {
MISSING_DEPENDENCIES=""
for program in $@; do
hash "$program" 2> /dev/null || MISSING_DEPENDENCIES="$program $MISSING_DEPENDENCIES"
done
if [[ -z "$MISSING_DEPENDENCIES" ]]; then
return
fi
if hash nix-env 2> /dev/null; then
nix-env -i $MISSING_DEPENDENCIES
elif hash pacman 2> /dev/null; then
if [[ "$(whoami)" == "root" ]]; then
pacman -Sy
pacman --noconfirm -S $MISSING_DEPENDENCIES
else
sudo pacman -Sy
sudo pacman --noconfirm -S $MISSING_DEPENDENCIES
fi
elif hash yum 2> /dev/null; then
if [[ "$(whoami)" == "root" ]]; then
yum install $MISSING_DEPENDENCIES
else
sudo yum install $MISSING_DEPENDENCIES
fi
elif hash apk 2> /dev/null; then
if [[ "$(whoami)" == "root" ]]; then
apk add $MISSING_DEPENDENCIES
else
sudo apk add $MISSING_DEPENDENCIES
fi
elif hash apt 2> /dev/null; then
sudo apt update
# Some docker containers are missing locales
if hash unminimize 2> /dev/null; then
sudo apt install -yq locales
locale-gen en_US.UTF-8
sudo localedef -i en_US -f UTF-8 en_US.UTF-8
fi
# Now actually install the dependencies
sudo apt install -yq $MISSING_DEPENDENCIES
fi
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment