]> arthur.barton.de Git - ax-unix.git/commitdiff
Add lib/ax/ax-common.sh
authorAlexander Barton <alex@barton.de>
Sat, 4 Apr 2015 21:09:04 +0000 (23:09 +0200)
committerAlexander Barton <alex@barton.de>
Sat, 4 Apr 2015 21:09:04 +0000 (23:09 +0200)
Currently this implements
 - ax_msg
 - ax_msg1
 - ax_abort

Makefile
lib/Makefile [new file with mode: 0644]
lib/ax/Makefile [new file with mode: 0644]
lib/ax/README [new file with mode: 0644]
lib/ax/ax-common.sh [new file with mode: 0644]

index 7327ace2d92c709f7c56fdde1a5d3785d13a6b91..5fa87e8b207428c7abffdf47dc58496acb843616 100644 (file)
--- 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 (file)
index 0000000..12d33ec
--- /dev/null
@@ -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 (file)
index 0000000..eac6db9
--- /dev/null
@@ -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 (file)
index 0000000..c55c01b
--- /dev/null
@@ -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 (file)
index 0000000..f060e4c
--- /dev/null
@@ -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
+}