From d8a18ce5e16e3e5dd53be25a03448019936c75a5 Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Sat, 4 Apr 2015 23:09:04 +0200 Subject: [PATCH] Add lib/ax/ax-common.sh Currently this implements - ax_msg - ax_msg1 - ax_abort --- Makefile | 4 ++-- lib/Makefile | 13 ++++++++++++ lib/ax/Makefile | 21 +++++++++++++++++++ lib/ax/README | 31 +++++++++++++++++++++++++++ lib/ax/ax-common.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 lib/Makefile create mode 100644 lib/ax/Makefile create mode 100644 lib/ax/README create mode 100644 lib/ax/ax-common.sh diff --git a/Makefile b/Makefile index 7327ace..5fa87e8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # # ax-unix: Alex' UNIX Tools & Scripts -# Copyright (c)2013-2014 Alexander Barton (alex@barton.de) +# Copyright (c)2013-2015 Alexander Barton (alex@barton.de) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -8,6 +8,6 @@ # (at your option) any later version. # -SUBDIRS = bup +SUBDIRS = lib bup include Makefile.ax diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..12d33ec --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,13 @@ +# +# ax-unix: Alex' UNIX Tools & Scripts +# Copyright (c)2013-2015 Alexander Barton (alex@barton.de) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + +SUBDIRS = ax + +include ../Makefile.ax diff --git a/lib/ax/Makefile b/lib/ax/Makefile new file mode 100644 index 0000000..eac6db9 --- /dev/null +++ b/lib/ax/Makefile @@ -0,0 +1,21 @@ +# +# ax-unix: Alex' UNIX Tools & Scripts +# Copyright (c)2013-2015 Alexander Barton (alex@barton.de) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + +ALL = README ax-common.sh + +include ../../Makefile.ax + +install-local: ax-common.sh + install -d -o $(USER) -g $(GROUP) -m 755 \ + $(DESTDIR)$(PREFIX)/lib/ax + install -p -o $(USER) -g $(GROUP) -m 755 README \ + $(DESTDIR)$(PREFIX)/lib/ax/README + install -p -o $(USER) -g $(GROUP) -m 755 ax-common.sh \ + $(DESTDIR)$(PREFIX)/lib/ax/ax-common.sh diff --git a/lib/ax/README b/lib/ax/README new file mode 100644 index 0000000..c55c01b --- /dev/null +++ b/lib/ax/README @@ -0,0 +1,31 @@ +ax-unix: Alex' UNIX Tools & Scripts +Copyright (c)2013-2015 Alexander Barton (alex@barton.de) + +This directory contains library files to use in 3rd party applications and +scripts. Please see below for usage information. + + +ax-common.sh +------------ + +This is a shell script library containing common useful functions. If you want +to use it in your own code, your script must include ("source") this library +file, and therefore it has to check some common locations (if you don't bundle +it in some other ways and exactly know the location it is installed in). + +Use something like the following in your scrips: + +----> cut here <--------------------------------------------------------------- +# Include "ax-common.sh": +for dir in "$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr; do + [ -z "$ax_common_sourced" ] || break + ax_common="${dir}/lib/ax/ax-common.sh" + [ -r "$ax_common" ] && source "$ax_common" +done +if [ -z "$ax_common_sourced" ]; then + echo "Error (`basename "$0"`): \"ax-common.sh\" not found, aborting!" + echo "Please install 'ax-unix', \"Alex' UNIX Tools & Scripts\", and try again." + exit 99 +fi +unset dir ax_common ax_common_sourced +----> cut here <--------------------------------------------------------------- diff --git a/lib/ax/ax-common.sh b/lib/ax/ax-common.sh new file mode 100644 index 0000000..f060e4c --- /dev/null +++ b/lib/ax/ax-common.sh @@ -0,0 +1,51 @@ +# +# ax-common.sh -- Common Functions for Shell Scripts +# Copyright (c)2013-2015 Alexander Barton (alex@barton.de) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + +ax_common_sourced=1 + +# Display a colored message. +# $1 Level: -=title, 0=ok, 1=warning, 2=error. +# $2 Word(s) to highlight. +# $3-n Remaining word(s). [optional] +ax_msg1() { + case "$1" in + "0") c="32"; shift; ;; # green + "1") c="33"; shift; ;; # yellow + "2") c="31"; shift; ;; # red + "-") c="1"; shift; ;; # bold + *) c="0"; + esac + # print colored word(s): + printf "\x1b[0;${c}m" + /bin/echo -n "${1}" + printf "\x1b[0m " + shift + # print remaining word(s) and trailing newline: + echo "${*}" +} + +# Display a colored message. +# $1 Level, see ax_msg1 function. +# $2-n Word(s) to highlight. +ax_msg() { + level="$1" + shift + ax_msg1 "$level" "$*" +} + +# Abort the script with an error message and exit code 1. +# $1 Error message [optional]. Will be formatted as "Error: %s Aborting!". +# if no error message is given, "Aborting!" will be printed. +ax_abort() { + [ $# -gt 0 ] \ + && ax_msg 2 "Error: $* Aborting!" \ + || ax_msg 2 "Aborting!" + exit 1 +} -- 2.39.2