Created
August 29, 2013 15:56
-
-
Save paulc/6379951 to your computer and use it in GitHub Desktop.
Useful /bin/sh functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_NORMAL=$(printf "\033[0m") | |
_RED=$(printf "\033[0;31m") | |
_CYAN=$(printf "\033[0;36m") | |
_log() { | |
local _cmd="$@" | |
printf "${COLOUR:+${_RED}}" | |
printf "%s [%s] %-40s\n" "$(date '+%b %d %T')" $name "CMD: $_cmd" | |
printf "${COLOUR:+${_CYAN}}" | |
eval "$_cmd" 2>&1 | sed -e 's/^/ | /' | |
local _status=$? | |
printf "${COLOUR:+${_RED}}" | |
[ $_status -eq 0 ] && printf "[OK]\n" || printf "[ERROR]\n" | |
printf "${COLOUR:+${_NORMAL}}" | |
return $_status | |
} | |
_backup() { | |
local _f=$1 | |
if [ ! -f ${_f}.ovh ] | |
then | |
printf "Backing up file: %s -> %s.ovh " $_f $_f | |
cp ${_f} ${_f}.ovh && printf "[OK]\n" | |
else | |
printf "Backup file %s.ovh exists\n" $_f | |
fi | |
} | |
_config() { | |
[ "$1" = "-echo" ] && shift && local _echo=1 | |
local _var="$1" | |
local _msg="$2" | |
local _default="$3" | |
local _r | |
if [ ${#_default} -gt 0 ] | |
then | |
read -p "${_msg} [${_default}]: " _r | |
else | |
read -p "${_msg}: " _r | |
fi | |
eval ${_var}=\"${_r:-${_default}}\" | |
[ "${_echo}" = 1 ] && eval echo \$${_var} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment