61 lines
1.8 KiB
Bash
61 lines
1.8 KiB
Bash
# ------------------------------------------------------------------------------
|
|
# Various utilitary functions
|
|
# This file is part of the init.sh project
|
|
# Copyright (c) 2019-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# ------------------------------------------------------------------------------
|
|
# This file is distributed under 3-clause BSD license.
|
|
# The complete license agreement can be obtained at:
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Define normalised time display, filename friendly
|
|
stdtime()
|
|
{
|
|
date --rfc-3339=seconds | sed -e 's/ /-/' -e 's/://g'
|
|
}
|
|
export -f stdtime
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Check if a function exists, return 0 if so
|
|
function_exists() {
|
|
if [[ $# -ne 1 ]]; then
|
|
prnt E "function_exists(): A function name is required!"
|
|
die 11 --force
|
|
fi
|
|
|
|
if [[ $(LC_ALL=C type -t $1 | grep function) ]]; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
export -f function_exists
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Get module name from module file
|
|
get_mod_name()
|
|
{
|
|
if [[ $# -ne 1 ]]; then
|
|
prnt E "get_mod_name(): Bad number of parameters."
|
|
die 11 --force
|
|
fi
|
|
echo $(basename $1 | cut -f 1 -d '.')
|
|
}
|
|
export -f get_mod_name
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Remove leading and trailing space of the given parameter
|
|
trim()
|
|
{
|
|
local string="$@"
|
|
echo "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${string}")"
|
|
unset string
|
|
}
|
|
export -f trim
|
|
|
|
# EOF
|