Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active May 13, 2022 01:01
Show Gist options
  • Save franz-josef-kaiser/df7dc6adf9489c40ceb1efaf7ac89b0c to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/df7dc6adf9489c40ceb1efaf7ac89b0c to your computer and use it in GitHub Desktop.
Bash installer for different OS

Bash Installer

If you are collaborating with others, chances that not everyone's on the same OS are good. If you want to avoid "running on my machine" kind of problems, just share an installer script for your favorite binaries installers/ upgrader/ uninstallers. This is one way to do it.

This is a simple installer script that selects different installation methods based on the OS you are running this on.

How to

Just replace @TODO with whatever you are installing. Make sure to adjust the different installation methods some project/ program offers.

Linux currently is running a Git clone installer that symlinks into some $PATH supporting place. This does not clone the complete repo, but just the last commit!

Uninstallation

Git

If you need to uninstall something you cloned via Git, just run rm -rf --preserve-root on the directory you cloned to. Maybe use echo rm -rf /path/to/dir first to check the output.

Homebrew

Just run brew uninstall foo or the same for cask.

Package Managers

Again, just refer to the usual process.

Upgrading

Git

Just run a git fetch followed by a git pull

Homebrew

The usual brew update followed by brew upgrade foo and brew cask upgrade foo. You can script that too.

Package Managers of all sorts

All other package managers support the same procdure: Fetch the most current list, then upgrade.

#!/usr/bin/env bash
# Install @TODO
# @link https://@TODO.example.com/link/to/docs
echo " > Installing @TODO - DETAILED NAME HERE…"
if command -v @TODO &> /dev/null
then
echo -e "\033[32m > @TODO already installed.\033[0m"
exit
fi
# OS Switch, author "Timmmm"
# @link https://stackoverflow.com/a/8597411/376483
# @license CC-BY-SA 4.0
# Linux
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Install
cd "${HOME}" || (echo " > Directory does not exist. Exiting." && exit)
mkdir -p "${HOME}/.local/bin/"
git clone --depth 1 git@github.com:@TODO.git ~/.@TODO
. "${HOME}/.profile"
# Set a symlink to something already in the $PATH
# to avoid cluttering the path
ln -s "${HOME}/.@TODO/bin/*" "${HOME}/.local/bin"
# Mac OSX
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Install via Homebrew or via Cask
brew cask install @TODO
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" ]]; then
# Chocolate package manager
command -v choco >/dev/null 2>&1 || {
choco install @TODO
}
# Else: You might need to copy from some other OS here.
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "win32" ]]; then
echo " > WTF. Win32."
# Free BSD… In case you really, really need it
elif [[ "$OSTYPE" == "freebsd"* ]]; then
pkg install @TODO
# WTF
else
echo -e "\033[31m > Unknown distribution. Nothing to install.\033[m"
exit
fi
echo -e "\033[32m > @TODO installed. Done!\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment