Added bash-support

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
2020-11-16 12:54:24 +01:00
parent 5b8989ac04
commit fc7dc3f34a
56 changed files with 12377 additions and 1 deletions

View File

@ -0,0 +1,21 @@
#=== FUNCTION ================================================================
# NAME: _trap_DEBUG
# DESCRIPTION: Trap code for the pseudo-signal DEBUG. Generate a message.
# The DEBUG trap is not inherited by functions.
# Use 'set -o functrace'
# PARAMETERS: 1) identification (e.g. line number $LINENO)
# 2) variable name(s) to be tracked
#===============================================================================
function _trap_DEBUG ()
{
declare identification=$1;
while [ ${#} -gt 1 ]; do
shift
echo -e "DEBUG [$identification] ${1} = '${!1}'"
done
} # ---------- end of function _trap_DEBUG ----------
trap '_trap_DEBUG $LINENO <-variable names->' DEBUG # trap DEBUG
#trap - DEBUG # reset the DEBUG trap

View File

@ -0,0 +1,15 @@
#=== FUNCTION ================================================================
# NAME: _trap_ERROR
# DESCRIPTION: Trap code for the pseudo-signal ERR (A command returning a
# non-zero exit status). Generates an error message.
# PARAMETERS: The current line number given by $LINENO .
#===============================================================================
function _trap_ERROR ()
{
echo -e "\nERROR line ${1}: Command exited with status ${?}"
} # ---------- end of function _trap_ERROR ----------
trap '_trap_ERROR $LINENO' ERR # trap ERR
#trap - ERR # reset the ERR trap

View File

@ -0,0 +1,14 @@
#=== FUNCTION ================================================================
# NAME: _trap_EXIT
# DESCRIPTION: Trap code for the pseudo-signal EXIT. Generates an message.
# PARAMETERS: The current line number given by $LINENO .
#===============================================================================
function _trap_EXIT ()
{
echo -e "\nEXIT line ${1}: Script exited with status ${?}"
} # ---------- end of function ----------
trap '_trap_EXIT $LINENO' EXIT # trap EXIT
#trap - EXIT # reset the EXIT trap

View File

@ -0,0 +1,17 @@
#=== FUNCTION ================================================================
# NAME: _trap_RETURN
# DESCRIPTION: Trap code for the pseudo-signal RETURN. Generates a message.
# The RETURN trap is not inherited by functions.
# Use 'set -o functrace'
# PARAMETERS: The current line number given by $LINENO .
# variable(s) to be tracked
#===============================================================================
function _trap_RETURN ()
{
echo -e "\nRETURN line ${1}: "
} # ---------- end of functionn _trap_RETURN ----------
trap '_trap_RETURN $LINENO' RETURN # trap RETURN
#trap - RETURN # reset the RETURN trap

View File

@ -0,0 +1,5 @@
basename=${pathname##*/}
dirname=${pathname%/*}
filename=${basename%%.*}
lastextension=${basename##*.}
allextensions=${basename#*.}

View File

@ -0,0 +1,3 @@
if [[ $number =~ ^[+-]?[0-9]+$ ]] ; then
echo -e "match found : (signed) integer\n"
fi

View File

@ -0,0 +1,3 @@
if [[ $number =~ ^[0-9]+$ ]] ; then
echo -e "match found : integer\n"
fi

View File

@ -0,0 +1,5 @@
#-----------------------------------------------------------------------
# Check number of command line arguments
#-----------------------------------------------------------------------
[ $# -lt 1 ] && { echo -e "\n\tUsage: ${0##/*/} File\n"; exit 1; }

View File

@ -0,0 +1,3 @@
TMPFILE=$( mktemp /tmp/example.XXXXXXXXXX ) || exit 1
rm --force $TMPFILE

View File

@ -0,0 +1,17 @@
TMPDIR=${TMPDIR:-/tmp} # defaults to /tmp if unset
#-------------------------------------------------------------------------------
# Creates a particular temporary directory inside $TMPDIR.
#-------------------------------------------------------------------------------
TEMPORARY_DIR=$(mktemp -d "$TMPDIR/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") || \
{ echo "ERROR creating a temporary file"; exit 1; }
#-------------------------------------------------------------------------------
# When the program exits, it tries to remove the temporary folder.
# This code is executed even if the process receives a signal 1,2,3 or 15.
#-------------------------------------------------------------------------------
trap '[ "$TEMPORARY_DIR" ] && rm --recursive --force "$TEMPORARY_DIR"' 0
touch $TEMPORARY_DIR/tempfile # new tempfile inside folder

View File

@ -0,0 +1,19 @@
#-----------------------------------------------------------------------
# cleanup temporary file in case of a keyboard interrupt (SIGINT)
# or a termination signal (SIGTERM)
#-----------------------------------------------------------------------
function cleanup_temp
{
[ -e $tmpfile ] && rm --force $tmpfile
exit 0
}
trap cleanup_temp SIGHUP SIGINT SIGPIPE SIGTERM
tmpfile=$(mktemp) || { echo "$0: creation of temporary file failed!"; exit 1; }
# use tmpfile ...
rm --force $tmpfile

View File

@ -0,0 +1,22 @@
# PS4 : timestamp; the current time in 24-hour HH:MM:SS format
PS4='+[\t] '
# PS4 : timestamp; 'seconds.nanoseconds' since 1970-01-01 00:00:00 UT
PS4='+[$(date "+%s.%N")] '
# PS4 : position, line number, function name
# The following line avoids error messages due to an unset FUNCNAME[0] :
# set +o nounset # Treat unset variables as an error
#
PS4='+|${BASH_SOURCE##*/} ${LINENO}${FUNCNAME[0]:+ ${FUNCNAME[0]}}| '
# PS4 : position, line number, function name, subshell information
# The following line avoids error messages due to an unset FUNCNAME[0] :
# set +o nounset # Treat unset variables as an error
#
PS4='+(${BASH_SOURCE##*/}: ${LINENO}) : ${FUNCNAME[0]} - [level ${SHLVL}, '
PS4=$PS4'subshell ${BASH_SUBSHELL}, return code $?]\n '
# PS4 : default prompt
PS4='+ '

View File

@ -0,0 +1,16 @@
#=== FUNCTION ================================================================
# NAME: _assert
# DESCRIPTION: Abort the script if assertion is false.
# PARAMETERS: 1) expression used as test inside brackets
# 2) optional information, e.g. $LINENO
# RETURNS: 0 / 99 assertion is true / false
#===============================================================================
function _assert ()
{
if [ ! $1 ]; then
echo "${0##*/}${2:+:$2}: assertion '$1' failed."
exit 99
fi
return 0
}

View File

@ -0,0 +1,15 @@
DEBUG=${DEBUG:-0} # 0 = no debug output, 1 = show debug output,
# or enable debug with: DEBUG=1 script.sh
#=== FUNCTION ================================================================
# NAME: _debug
# DESCRIPTION: echo debug output controlled by a global variable
# PARAMETERS: list, e.g.; "$LINENO: x=$x"
# RETURNS: always 0
#===============================================================================
_debug ()
{
[ ${DEBUG} -ne 0 ] && echo -e "${@}"
return 0
} # ---------- end of function _debug ----------

View File

@ -0,0 +1,3 @@
# print the positional parameters
printf "'%b'\n" "$0" "$@" | nl -v0 -s': '

View File

@ -0,0 +1,16 @@
DEBUG=${DEBUG:-0} # 0 = no debug output, 1 = show debug output,
# or enable debug with: DEBUG=1 script.sh
#=== FUNCTION ================================================================
# NAME: _debug_timestamp
# DESCRIPTION: timestamp + optional information
# timestamp: {seconds since 1970-01-01 00:00:00}.{nanoseconds}
# PARAMETERS: identification, e.g. $LINENO (optional)
# RETURNS: always 0
#===============================================================================
_debug_timestamp ()
{
[ ${DEBUG} -ne 0 ] && echo -e "[ $(date "+%s.%N") ]${@:+ -- ${@}}"
return 0
} # ---------- end of function _debug_timestamp ----------

View File

@ -0,0 +1,8 @@
#
#==========================================================================
# 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.
#==========================================================================
#

View File

@ -0,0 +1,3 @@
timestamp=$(date +"%Y%m%d-%H%M%S") # generate timestamp : YYYYMMDD-hhmmss

View File

@ -0,0 +1,39 @@
ScriptVersion="1.0"
#=== FUNCTION ================================================================
# NAME: usage
# DESCRIPTION: Display usage information.
#===============================================================================
function usage ()
{
cat <<- EOT
Usage : ${0##/*/} [options] [--]
Options:
-h|help Display this message
-v|version Display script version
EOT
} # ---------- end of function usage ----------
#-----------------------------------------------------------------------
# Handle command line arguments
#-----------------------------------------------------------------------
while getopts ":hv" opt
do
case $opt in
h|help ) usage; exit 0 ;;
v|version ) echo "$0 -- Version $ScriptVersion"; exit 0 ;;
\? ) echo -e "\n Option does not exist : $OPTARG\n"
usage; exit 1 ;;
esac # --- end of case ---
done
shift $(($OPTIND-1))

View File

@ -0,0 +1,15 @@
infilename="" # input filename
exec 3<"$infilename"
if [ $? -ne 0 ] ; then
echo -e "Could not link file descriptor with file '$infilename'\n"
exit 1
fi
while read line <&3 ; do
echo -e "$line"
done
exec 3<&- # close file descriptor

View File

@ -0,0 +1,13 @@
outfilename="" # output filename
exec 4>"$outfilename"
if [ $? -ne 0 ] ; then
echo -e "Could not link file descriptor with file '$outfilename'\n"
exit 1
fi
echo -e "text" >&4
exec 4>&- # close file descriptor

View File

@ -0,0 +1,29 @@
set -o nounset # treat unset variables as errors
#===============================================================================
# GLOBAL DECLARATIONS
#===============================================================================
declare -rx SCRIPT=${0##*/} # the name of this script
declare -rx mkdir='/bin/mkdir' # the mkdir(1) command
#===============================================================================
# SANITY CHECKS
#===============================================================================
if [ -z "$BASH" ] ; then
printf "$SCRIPT:$LINENO: run this script with the BASH shell\n" >&2
exit 192
fi
if [ ! -x "$mkdir" ] ; then
printf "$SCRIPT:$LINENO: command '$mkdir' not available - aborting\n" >&2
exit 192
fi
#===============================================================================
# MAIN SCRIPT
#===============================================================================
#===============================================================================
# STATISTICS / CLEANUP
#===============================================================================
exit 0