Skip to content

Instantly share code, notes, and snippets.

@sydseter
Last active August 29, 2015 14:04
Show Gist options
  • Save sydseter/6574fab45198e3901822 to your computer and use it in GitHub Desktop.
Save sydseter/6574fab45198e3901822 to your computer and use it in GitHub Desktop.
Common bash functions for creating install scripts
#!/usr/bin/env bash
set -o nounset
set -o errtrace
set -o errexit
set -o pipefail
# Text color variables
declare -r txtund=$(tput sgr 0 1) # Underline
declare -r txtbld=$(tput bold) # Bold
declare -r failColor=${txtbld}$(tput setaf 1) # red
declare -r warnColor=${txtbld}$(tput setaf 3) # yellow
declare -r notice=${txtbld}$(tput setaf 4) # blue
declare -r exampleColor=${txtbld}$(tput setaf 2) # green
declare -r infoColor=${txtbld}$(tput setaf 7) # white
declare -r txtrst=$(tput sgr0) # Reset
declare -r isNumber='^[0-9]+$';
function prompt_yes_no () {
local choice;
builtin read -p "${notice}$1(y/n): ${txtrst}" -r choice;
case $choice in
y|Y) echo "yes";;
n|N) echo "no";;
*) echo "invalid";;
esac
}
function say () {
printf "${infoColor}%b${txtrst}\n" "$*";
}
function fail () {
printf "${failColor}ERROR: %b${txtrst}\n" "$*";
exit 1;
}
function warn () {
printf "${warnColor}WARN: %b${txtrst}\n" "$*";
}
function example () {
printf "${exampleColor}%b${txtrst}\n" "$*";
}
function prompt_string () {
local answer;
builtin read -p "${notice}$1: ${txtrst}" -r answer;
if [ ! -z "$answer" ]; then
echo $answer;
return 0;
else
return 1;
fi
}
function wait_for_keypress () {
local answer;
builtin read -n 1 -p "${notice}Press any key to continue${txtrst}" -r answer;
}
function open_url () {
local -r url=$1;
case "$(uname)" in
Darwin)
open $1;
;;
Linux)
BROWSER=${BROWSER:-};
if [ ! -z "$BROWSER" ]; then
$BROWSER "$url";
elif which xdg-open > /dev/null; then
xdg-open "$url";
elif which gnome-open > /dev/null; then
gnome-open "$url";
else
fail "Could not detect the web browser to use. Please visit $url manually.";
fi
;;
*)
fail "Don't know how to open a browser on this platform. Please visit $url manually.";
;;
esac
}
function abort_not_installed () {
local -r progname=$1;
local -r prog_url=$2;
say "$progname doesn't seem to be installed!";
local answer=$(prompt_yes_no \
"Do you want to visit the $progname website to download and install it?");
[[ $answer == "yes" ]] && open_url $prog_url;
say "\nTry running this program again after you have installed $progname!";
exit 2;
}
function abort_too_old () {
local -r progname=$1;
local -r prog_url=$2;
local -r minver=$3;
say "$progname is too old (need at least $minver)!";
local answer=$(prompt_yes_no \
"Do you want to visit the $progname website to upgrade it?");
[[ $answer == "yes" ]] && open_url $prog_url;
say "\nTry running this program again after you have upgraded $progname!";
exit 2;
}
# http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ! [[ ${ver1[i]} =~ $isNumber ]] ; then
return 3;
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
function found () {
hash $1 2>&-;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment