@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,5 @@
|
||||
basename=${pathname##*/}
|
||||
dirname=${pathname%/*}
|
||||
filename=${basename%%.*}
|
||||
lastextension=${basename##*.}
|
||||
allextensions=${basename#*.}
|
@ -0,0 +1,3 @@
|
||||
if [[ $number =~ ^[+-]?[0-9]+$ ]] ; then
|
||||
echo -e "match found : (signed) integer\n"
|
||||
fi
|
@ -0,0 +1,3 @@
|
||||
if [[ $number =~ ^[0-9]+$ ]] ; then
|
||||
echo -e "match found : integer\n"
|
||||
fi
|
@ -0,0 +1,5 @@
|
||||
#-----------------------------------------------------------------------
|
||||
# Check number of command line arguments
|
||||
#-----------------------------------------------------------------------
|
||||
[ $# -lt 1 ] && { echo -e "\n\tUsage: ${0##/*/} File\n"; exit 1; }
|
||||
|
@ -0,0 +1,3 @@
|
||||
TMPFILE=$( mktemp /tmp/example.XXXXXXXXXX ) || exit 1
|
||||
|
||||
rm --force $TMPFILE
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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='+ '
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 ----------
|
@ -0,0 +1,3 @@
|
||||
# print the positional parameters
|
||||
printf "'%b'\n" "$0" "$@" | nl -v0 -s': '
|
||||
|
@ -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 ----------
|
@ -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.
|
||||
#==========================================================================
|
||||
#
|
@ -0,0 +1,3 @@
|
||||
|
||||
timestamp=$(date +"%Y%m%d-%H%M%S") # generate timestamp : YYYYMMDD-hhmmss
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
Reference in New Issue
Block a user