- Base library:
- Rename default.bash to base.bash,
- Normalise some function names: version_print(), help_print(), str_quote(), str_escape(), echo_line(), echo_error() & cmd_exec()
- Normalise some global variables: VERBOSE & DRY_RUN,
- Add default options: errexit, pipefail & nounset,
- Add GPL headers,
- ISL:
- Move ISL functions to isl command,
- Add bash completion,
- URPMI-Setup:
- Add urpmi-setup command.
This commit is contained in:
445
usr/lib/rx3/base.bash
Normal file
445
usr/lib/rx3/base.bash
Normal file
@@ -0,0 +1,445 @@
|
||||
#!/bin/bash
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
#
|
||||
# Rx3 Bash Base Library
|
||||
#
|
||||
# Copyright (C) 2017-2026 Arnaud G. GIBERT
|
||||
# mailto:arnaud@rx3.net
|
||||
#
|
||||
# This is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this program; If not, see
|
||||
# <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
if [[ "${DEFAULT_BASH}" != "" ]]
|
||||
then
|
||||
return
|
||||
else
|
||||
declare -g DEFAULT_BASH=1
|
||||
fi
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Default Options
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
set -o errexit -o pipefail -o nounset
|
||||
shopt -s extglob
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Global Variable
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
declare -g VERSION=""
|
||||
declare -g NAME=""
|
||||
declare -g HELP=""
|
||||
declare -g VERBOSE="FALSE"
|
||||
declare -g DRY_RUN="FALSE"
|
||||
|
||||
declare -g LOG_FILE=""
|
||||
declare -g LOG_LOCK=""
|
||||
declare -g LOG_ECHO=""
|
||||
declare -g LOG_TRACE="DISABLED"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Version Print
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
version_print()
|
||||
{
|
||||
# echo "${VERSION}" | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//'
|
||||
echo "${NAME} V${VERSION}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Help Print
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
help_print()
|
||||
{
|
||||
echo "${NAME} ${HELP}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Str Quote
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
str_quote()
|
||||
{
|
||||
local quoted=${1//\'/\'\\\'\'}
|
||||
|
||||
|
||||
printf "'%s'" "${quoted}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Str Escape
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
str_escape()
|
||||
{
|
||||
echo "$*" | sed -e "s/\"/\\\\\"/g"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Echo Line
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
echo_line()
|
||||
{
|
||||
local string="$1"
|
||||
local count="$2"
|
||||
|
||||
|
||||
echo -en "\e[2K\r"
|
||||
|
||||
if [[ "${count}" != "" ]]
|
||||
then
|
||||
printf "%05d: %s" "${count}"
|
||||
echo -en "${string}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Echo Error
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
echo_error()
|
||||
{
|
||||
echo "$@" 1>&2
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Cmd Exec
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
cmd_exec()
|
||||
{
|
||||
local cmd="$1"
|
||||
|
||||
|
||||
if [[ "${VERBOSE}" == "TRUE" ]]
|
||||
then
|
||||
echo "${cmd}" 1>&2
|
||||
fi
|
||||
|
||||
if [[ "${DRY_RUN}" != "TRUE" ]]
|
||||
then
|
||||
eval "${cmd}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Tab Assign
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
tab_assign()
|
||||
{
|
||||
declare -n ta_tab=$1
|
||||
ta_key=$2
|
||||
ta_value=$3
|
||||
|
||||
|
||||
if [[ "${ta_value}" == "-" ]]
|
||||
then
|
||||
ta_value=""
|
||||
fi
|
||||
|
||||
ta_tab[${ta_key}]="${ta_value}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Var Assign
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
var_assign()
|
||||
{
|
||||
declare -n va_var=$1
|
||||
va_value=$2
|
||||
va_mode=$3
|
||||
|
||||
|
||||
if [[ "${va_value}" == "-" ]]
|
||||
then
|
||||
va_value=""
|
||||
fi
|
||||
|
||||
if [[ "${va_mode}" == "INC" ]]
|
||||
then
|
||||
va_var="${va_var} ${va_value}"
|
||||
else
|
||||
va_var="${va_value}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# File Dir Init
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_dir_init()
|
||||
{
|
||||
local File="$1"
|
||||
local Owner="$2"
|
||||
local Group="$3"
|
||||
|
||||
local dir
|
||||
|
||||
|
||||
if [ ! -f ${File} ]
|
||||
then
|
||||
if [[ "$( id -u)" != "0" ]]
|
||||
then
|
||||
${ECHO} "Can't perform file init of: [${File}] as non root user!"
|
||||
else
|
||||
dir="$( dirname ${File})"
|
||||
|
||||
if [ ! -d ${dir} ]
|
||||
then
|
||||
${ECHO} "Initializing directory: [${dir}]"
|
||||
|
||||
mkdir ${dir}
|
||||
chmod ug+rwx ${dir}
|
||||
chown ${Owner}:${Group} ${dir}
|
||||
fi
|
||||
|
||||
${ECHO} "Initializing file: [${File}]"
|
||||
|
||||
>${File}
|
||||
chmod ug+rw ${File}
|
||||
chown ${Owner}:${Group} ${File}
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# File Lock
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_lock()
|
||||
{
|
||||
local file="$1"
|
||||
local mode="$2"
|
||||
local desc="$3"
|
||||
|
||||
|
||||
if [[ ( "${mode}" == "EXCLUSIVE" ) || ( "${mode}" == "WRITE" ) ]]
|
||||
then
|
||||
flag="-x"
|
||||
else
|
||||
flag="-s"
|
||||
fi
|
||||
|
||||
if [[ "${desc}" == "" ]]
|
||||
then
|
||||
desc="9"
|
||||
fi
|
||||
|
||||
|
||||
eval "exec ${desc}<>\"\${file}\""
|
||||
|
||||
if ! flock ${flag} -w 5 ${desc}
|
||||
then
|
||||
err_echo "Failed to acquire [${mode}] lock on: [${file}]"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# File Unlock
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_unlock()
|
||||
{
|
||||
local desc="$1"
|
||||
|
||||
|
||||
if [[ "${desc}" == "" ]]
|
||||
then
|
||||
desc="9"
|
||||
fi
|
||||
|
||||
|
||||
eval "exec ${desc}<&-"
|
||||
eval "exec ${desc}>&-"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Set
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_set()
|
||||
{
|
||||
local log_file="$1"
|
||||
local lock_file="$2"
|
||||
local echo_function="$3"
|
||||
local log_trace="$4"
|
||||
|
||||
|
||||
LOG_FILE="${log_file}"
|
||||
LOG_LOCK="${lock_file}"
|
||||
LOG_ECHO="${echo_function}"
|
||||
|
||||
if [[ ${log_trace} != "" ]]
|
||||
then
|
||||
LOG_TRACE="${log_trace}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Print
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_print()
|
||||
{
|
||||
local log_file="$1"
|
||||
local lock_file="$2"
|
||||
local echo_function="$3"
|
||||
local log_type="$4"
|
||||
local log_prefix="$5"
|
||||
|
||||
|
||||
shift; shift; shift; shift; shift
|
||||
|
||||
if [[ "${log_type}" != "TRA" ]] || [[ "${LOG_TRACE}" != "DISABLED" ]]
|
||||
then
|
||||
${echo_function} "($BASHPID):" "$*"
|
||||
|
||||
if [[ "${log_file}" != "" ]]
|
||||
then
|
||||
if [[ "${lock_file}" != "" ]]
|
||||
then
|
||||
file_lock "${lock_file}" WRITE 8
|
||||
fi
|
||||
|
||||
printf >> "${log_file}" "%s %9s %3s %16s %s\n" "$(date --rfc-3339=seconds -u)" "($BASHPID)" "${log_type}" "${log_prefix}:" "$*"
|
||||
|
||||
if [[ "${lock_file}" != "" ]]
|
||||
then
|
||||
file_unlock 8
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Trace
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_trace()
|
||||
{
|
||||
log_print "${LOG_FILE}" "${LOG_LOCK}" "${LOG_ECHO}" "TRA" "$@"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Info
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_info()
|
||||
{
|
||||
log_print "${LOG_FILE}" "${LOG_LOCK}" "${LOG_ECHO}" "INF" "$@"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Warning
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_warning()
|
||||
{
|
||||
log_print "${LOG_FILE}" "${LOG_LOCK}" "${LOG_ECHO}" "WRN" "$@"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# Log Error
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_error()
|
||||
{
|
||||
log_print "${LOG_FILE}" "${LOG_LOCK}" "${LOG_ECHO}" "ERR" "$@"
|
||||
}
|
||||
Reference in New Issue
Block a user