Skip to content

Instantly share code, notes, and snippets.

@Zerogiven
Last active October 2, 2016 13:07
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Zerogiven/3b4db68ba803f1a104b5 to your computer and use it in GitHub Desktop.
Functions which i need in my bash scripts but could also be nice for other ones
#!/bin/bash
# CSoellinger
######################################################
# Function Vars
APT_INSTALL="apt-get install -yqqq"
APT_UPDATE="apt-get update -yqqq"
NPM_INSTALL="npm --loglevel=silent install -g"
TEXT_DONE="DONE"
TEXT_WAITING_DOTS="... "
######################################################
# Echos
_echo() { echo -e "$1"; }
_echo_not_root() { _echo_and_exit "ERROR: You have to be root to execute this script"; }
_echo_root() { _echo_and_exit "ERROR: Do not execute this script as root user"; }
_echo_exec_start() { echo -ne "${1}${TEXT_WAITING_DOTS}"; }
_echo_exec_end() { echo -e "${TEXT_DONE}"; }
_echo_exec() { _echo_exec_start "$1"; $2; _echo_exec_end; }
_echo_and_exit() {
[ "$2" == "" ] && EXIT_NR="1" || EXIT_NR="$2"
echo -e "$1"
exit $EXIT_NR
}
_apt_install() { _echo_exec_start "$1"; $APT_INSTALL $2 2>&1 >/dev/null; _echo_exec_end; }
_apt_update() { _echo_exec_start "Updating apt"; $APT_UPDATE 2>&1 >/dev/null; _echo_exec_end; }
_npm_install() { _echo_exec_start "$1"; $NPM_INSTALL $2; _echo_exec_end; }
_command_exists() { type "$1" &> /dev/null ; }
_fetch_bashrc_name() {
SHELLPATH="$SHELL"
case "${SHELLPATH##*/}" in
zsh )
echo ".zshrc";;
bash )
echo ".bashrc";;
esac
exit 0
}
_chown_recursive_from_target() {
[ ! "$1" ] && exit 0
TARGET_OWNER=`ls -ld $NVM_DIR | awk '{print $3}'`
chown $TARGET_OWNER:$TARGET_OWNER $1 -R
}
_is_online() {
if ping -w 1 -i 0.2 8.8.8.8 > /dev/null; then
echo "ONLINE"
else
if [ `_is_online_resolve_hostname` ]; then
echo "ONLINE"
#else
# echo "OFFLINE"
fi
fi
exit 0
}
_is_online_resolve_hostname() {
if ! _command_exists host; then exit 0; fi
if 2>&1 host -h | grep -q -- -W; then command="host -W 1"; else command="host -s 1"; fi
if $command google-public-dns-a.googxxle.com > /dev/null; then
echo "ONLINE"
#else
# echo "OFFLINE"
fi
exit 0
}
_is_composer_installed() {
composer -v > /dev/null 2>&1
echo $?
exit 0
}
_exit_if_not_root() { [ $EUID != 0 ] && _echo_not_root; }
_exit_if_root() { [ $EUID == 0 ] && _echo_root; }
######################################################
# Function "alias"..
_e() { _echo "$1"; }
_nl() { _echo ""; }
_not_root() { _echo_not_root; }
_exit() { _echo_and_exit $1; }
_exec_start() { _echo_exec_start "$1"; }
_exec_end() { _echo_exec_end; }
_exec() { _echo_exec "$1" "$2"; }
_cmd_exists() { _command_exists $1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment