Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created September 18, 2011 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanmix51/1225256 to your computer and use it in GitHub Desktop.
Save chanmix51/1225256 to your computer and use it in GitHub Desktop.
Bash library to develop scripts
. $LIB_DIR/colors.sh
error_msg() {
echo -e "${COLOR_RED}ERROR:${COLOR_RESET} $1 " >&2;
}
notice() {
echo -e "${COLOR_BOLD}notice:${COLOR_RESET} $1" >&2;
}
warning() {
echo -e "${COLOR_YELLOW}warning:${COLOR_RESET} $1" >&2;
}
error_and_exit() {
local switch=$1
shift;
case "$switch" in
"1")
help;
;;
"2")
error_msg "$1";
;;
"3")
error_msg "$1";
help;
;;
"4")
error_code=${1:-99};
exit $error_code;
;;
"5")
help;
error_code=${1:-99};
exit $error_code;
;;
"6")
error_msg "$1";
shift;
error_code=${1:-99};
exit $error_code;
;;
"7")
error_msg "$1";
help;
shift;
error_code=${1:-99};
exit $error_code;
;;
esac;
}
must() {
if ! eval "$1";
then
error_msg "$2";
return 1;
fi
return 0;
}
must_not() {
if eval "$1";
then
error_msg "$2";
return 1;
fi
return 0;
}
check_arg_count() {
local count=$1;
shift;
if [ $# -lt $count ];
then
error_and_exit 7 "More arguments required.";
fi
}
must_be_root() {
if [ $(id -u) -ne 0 ];
then
error_and_exit 6 "Must be super user.";
fi
}
create_password() {
local pass=$(must "apg -a 0 -M Ncl -n 6 -x 8 -m 3 -d" "Could not generate password.") \
|| return $?;
echo $pass;
}
COLOR_RESET="\033[0m";
COLOR_BOLD="\033[1m";
COLOR_UNDERLINE="\033[4m";
COLOR_REVERSE="\033[7m";
COLOR_BLACK="\033[1;30m";
COLOR_RED="\033[1;31m";
COLOR_GREEN="\033[1;32m";
COLOR_YELLOW="\033[1;33m";
COLOR_BLUE="\033[1;34m";
COLOR_PURPLE="\033[1;35m";
COLOR_CYAN="\033[1;36m";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment