3 Commits

  1. 9
      README.md
  2. 18
      bash.bashrc
  3. 79
      bin/miaou-bash
  4. 2
      install.sh
  5. 19
      lib/functions.bash

9
README.md

@ -1,8 +1,8 @@
MIAOU-BASH
===========
MIAOU-BASH is a collection of settings and helpers for leveraging BASH.
Developer-friendly, it may be used as solo package with or without the miaou project.
MIAOU-BASH is a collection of settings and helpers aiming for leveraging BASH.
Developer-friendly, it may be used as a solo package or combined with other miaou-* utilities.
## Featuring
@ -16,19 +16,22 @@ It's Free Software (AGPLv3), help yourself.
Any feedback would be appreciated. [contact@artcode.re](mailto:contact@artcode.re)
## install
`curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s`
## upgrade
`upgrade-miaou-bash`
## uninstall
`cd /opt/miaou-bash && sudo ./uninstall.sh`
## development mode (credentials required)
* REAL_DIR=/opt/miaou-bash
* sudo mkdir -m 755 $REAL_DIR && sudo chown $(id -un) $REAL_DIR
* git clone git@artcode.re:miaou/miaou-bash.git $REAL_DIR
* git clone <git@artcode.re>:miaou/miaou-bash.git $REAL_DIR
* DEV_DIR=$HOME/DEV/BASH/miaou-bash # change DEST according to your needs!
* ln -s $REAL_DIR $DEV_DIR
* cd $REAL_DIR && sudo ./install.sh

18
bash.bashrc

@ -149,15 +149,21 @@ function __vte_urlencode {
### MAIN
###------
MIAOU_BASH_DIR=$(realpath /opt/miaou-bash)
export MIAOU_BASH_DIR
## Add path for TOOLBOX if any
[[ -d "/TOOLBOX" ]] && PATH=$PATH:/TOOLBOX || true
# Add path to any tools folder in /opt/miaou-*
for i in {/opt,$HOME}/miaou-*/tools; do [[ -d "$i" ]] && PATH=$PATH:$i; done || true
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return ;;
esac
MIAOU_BASH_DIR=$(realpath /opt/miaou-bash)
export MIAOU_BASH_DIR
HOSTNAME=$(hostname -f)
# don't put duplicate lines or lines starting with space in the history.
@ -232,9 +238,3 @@ alias ssu4="ss -ntel4p | perl -pne 'if(/uid:(\\d+)/){@a=getpwuid(\$1);s/uid:(\\d
# Alias definitions.
[[ -f "/etc/bash.aliases" ]] && source "/etc/bash.aliases" || true
[[ -f "$HOME/.bash_aliases" ]] && source "$HOME/.bash_aliases" || true
## Add path for TOOLBOX if any
[[ -d "/TOOLBOX" ]] && PATH=$PATH:/TOOLBOX || true
# Add path to any tools folder in /opt/miaou-*
for i in {/opt,$HOME}/miaou-*/tools; do [[ -d "$i" ]] && PATH=$PATH:$i; done || true

79
bin/miaou-bash

@ -1,14 +1,17 @@
#!/usr/bin/env bash
# CONSTANTS
BASH_BACK_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u))
BASH_BACK_DEBUG=${BASH_BACK_DEBUG:-false}
MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)")
MIAOU_BASH_DEBUG=${MIAOU_BASH_DEBUG:-false}
readonly MIAOU_BASH_ERROR MIAOU_BASH_DEBUG
# FUNCTIONS
function on_return {
# regex does not work here unfortunately!
if [[ "${BASH_COMMAND}" == 'return '* ]]; then
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) # regex does not work here unfortunately! thus use of basic cut
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2)
if [[ -n "$code" ]] && [[ "$code" -ne 0 ]]; then
trap - DEBUG
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}"
@ -16,12 +19,12 @@ function on_return {
fi
}
# overridden function which permits stacktracing of original `return` statement
# overridden function which permits backtracing of original `return` statement
function exit {
code=${1:-0}
>&2 echo "${BASH_SOURCE[2]}:${BASH_LINENO[0]} exit $@"
# >&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: exit $@"
builtin exit $code
>&2 echo "${BASH_SOURCE[2]}:${BASH_LINENO[0]} exit $code"
builtin exit "$code"
}
function on_exit {
@ -31,19 +34,19 @@ function on_exit {
stack_functions=("${FUNCNAME[@]}")
stack_sources=("${BASH_SOURCE[@]}")
unset stack_lines[-1]
unset stack_lines[-1]
unset stack_functions[0]
unset stack_functions[-1]
unset 'stack_lines[-1]'
unset 'stack_lines[-1]'
unset 'stack_functions[0]'
unset 'stack_functions[-1]'
stack_functions[-1]='<main>'
unset stack_sources[0]
unset stack_sources[-1]
unset 'stack_sources[0]'
unset 'stack_sources[-1]'
if [[ "${stack_functions[1]}" == exit && "${stack_sources[1]}" == "${BASH_SOURCE[0]}" ]]; then
# the exit case scenario
unset stack_functions[1]
unset stack_sources[1]
unset stack_lines[0]
unset 'stack_functions[1]'
unset 'stack_sources[1]'
unset 'stack_lines[0]'
fi
stack_lines=("${stack_lines[@]}")
@ -53,9 +56,9 @@ function on_exit {
stack_summary=()
if [[ $code -gt 0 ]]; then
last_error=$(tail -n1 $BASH_BACK_ERROR)
last_error=$(tail -n1 "$MIAOU_BASH_ERROR")
last_source=${stack_sources[0]}
last_source=${last_source//./"\."} # replace . by \.
last_source=${last_source//./"\."} # replace '.' with '\.'
regex1="^$last_source: line ([0-9]+): (.*)\$"
regex2="^$last_source:([0-9]+) (.*)\$"
if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then
@ -85,57 +88,59 @@ function on_exit {
stack_detail=$(printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}")
stack_summary+=("$stack_detail")
done
>&4 dump_stderr true
dump_summary
dump_failure
else
>&4 dump_stderr false
dump_success
fi
cleanup
builtin exit $code
builtin exit "$code"
}
function dump_summary {
$BASH_BACK_DEBUG && >/dev/tty echo '---stacktrace---'
function dump_failure {
dump_stderr true
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stacktrace---'
for i in "${stack_summary[@]}"; do
>&4 echo -e "$i"
done
cleanup
}
function dump_stderr {
last_trim=${1:-false}
mapfile -t tmp_error <$BASH_BACK_ERROR
$last_trim && unset tmp_error[-1] && tmp_error=("${tmp_error[@]}")
mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
$last_trim && unset 'tmp_error[-1]' && tmp_error=("${tmp_error[@]}")
if [[ "${#tmp_error[@]}" -gt 0 ]]; then
$BASH_BACK_DEBUG && >/dev/tty echo '---stderr---'
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stderr---'
for i in "${tmp_error[@]}"; do
echo -e "$i"
>&4 echo -e "$i"
done
fi
}
function success {
>&2 dump_stderr
function dump_success {
dump_stderr
cleanup
}
function cleanup {
trap - ERR TERM INT EXIT
exec 3>&- 4>&-
rm $BASH_BACK_ERROR
rm "$MIAOU_BASH_ERROR"
}
# MAIN
set -TEue -o pipefail
[[ $# -ne 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1
[[ $# -lt 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1
trap 'on_exit $?' ERR TERM INT EXIT
trap 'on_return' DEBUG
BASH_ARGV0="$1" && shift # to pretend script runs by itself
# magic FD
exec 3> >(tee $BASH_BACK_ERROR >/dev/null) 4>/dev/stderr
# magic FD + delayed tee => /dev/fd/3 means delayed stderr, /dev/fd/4 remains original stderr
exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr
$BASH_BACK_DEBUG && >/dev/tty echo '---stdout---'
source $1 2>&3
success
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stdout---'
# shellcheck source=/dev/null
source "$BASH_ARGV0" 2>&3
dump_success

2
install.sh

@ -3,7 +3,7 @@
## CONSTANTS
readonly CURDIR=$PWD
readonly REQUIRED_PKGS=(file git bc vim jq)
readonly REQUIRED_PKGS=(file git bc vim jq bat gum)
readonly MAIN_TAR_GZ_URL="https://git.artcode.re/miaou/miaou-bash/archive/main.tar.gz"
## FUNCTIONS

19
lib/functions.bash

@ -59,6 +59,25 @@ function _confirm_destructive {
esac
}
## =================
## OPTIONS Functions
## =================
function _flatten_short_options {
local -n args=$1
local result=()
for word in ${args[@]}; do
[[ $word == -- ]] && break
if [[ $word =~ ^-[a-z][a-z] ]]; then
word=${word:1}
for ((i = 0; i < ${#word}; i++)); do result+=("-${word:i:1}"); done
else
result+=("$word")
fi
done
args=("${result[@]}")
}
## ==============
## Deprecated: FIXME: to remove
## ==============

Loading…
Cancel
Save