- Default library:
- Normalize 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 to docker_tools.
This commit is contained in:
@@ -1,4 +1,26 @@
|
||||
#!/bin/bash
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
#
|
||||
# Default 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
|
||||
@@ -9,69 +31,84 @@ fi
|
||||
|
||||
|
||||
|
||||
# Global Variable
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
declare -g LOG_FILE=""
|
||||
declare -g LOG_LOCK=""
|
||||
declare -g LOG_ECHO=""
|
||||
declare -g LOG_TRACE="DISABLED"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Default Options
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
set -o errexit -o pipefail -o nounset
|
||||
shopt -s extglob
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Print Version
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# 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 "${VERSION}" | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//'
|
||||
echo "${NAME} ${VERSION}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Prin Help
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Help Print
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
help_print()
|
||||
{
|
||||
echo "${NAME} ${HELP}"
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Quote Str
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Str Quote
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
quote_str()
|
||||
str_quote()
|
||||
{
|
||||
local quoted=${1//\'/\'\\\'\'}
|
||||
printf "'%s'" "$quoted"
|
||||
|
||||
|
||||
printf "'%s'" "${quoted}"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Escape Str
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Str Escape
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
escape_str()
|
||||
str_escape()
|
||||
{
|
||||
echo "$*" | sed -e "s/\"/\\\\\"/g"
|
||||
}
|
||||
@@ -80,14 +117,16 @@ escape_str()
|
||||
|
||||
|
||||
|
||||
# Line Echo
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Echo Line
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
line_echo()
|
||||
echo_line()
|
||||
{
|
||||
string="$1"
|
||||
count="$2"
|
||||
local string="$1"
|
||||
local count="$2"
|
||||
|
||||
|
||||
echo -en "\e[2K\r"
|
||||
|
||||
if [[ "${count}" != "" ]]
|
||||
@@ -101,10 +140,11 @@ line_echo()
|
||||
|
||||
|
||||
|
||||
# Err Echo
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Echo Error
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
err_echo()
|
||||
echo_error()
|
||||
{
|
||||
echo "$@" 1>&2
|
||||
}
|
||||
@@ -113,20 +153,21 @@ err_echo()
|
||||
|
||||
|
||||
|
||||
# Exec CMD
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# Cmd Exec
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
exec_cmd()
|
||||
cmd_exec()
|
||||
{
|
||||
cmd="$1"
|
||||
local cmd="$1"
|
||||
|
||||
|
||||
if [[ "${verbose}" == "true" ]]
|
||||
if [[ "${VERBOSE}" == "TRUE" ]]
|
||||
then
|
||||
echo "${cmd}" 1>&2
|
||||
fi
|
||||
|
||||
if [[ "${dry_run}" != "true" ]]
|
||||
if [[ "${DRY_RUN}" != "TRUE" ]]
|
||||
then
|
||||
eval "${cmd}"
|
||||
fi
|
||||
@@ -136,7 +177,7 @@ exec_cmd()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# tab_assign
|
||||
# Tab Assign
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
tab_assign()
|
||||
@@ -159,7 +200,7 @@ tab_assign()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# var_assign
|
||||
# Var Assign
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
var_assign()
|
||||
@@ -187,7 +228,7 @@ var_assign()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# file_dir_init
|
||||
# File Dir Init
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_dir_init()
|
||||
@@ -230,7 +271,7 @@ file_dir_init()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# file_lock
|
||||
# File Lock
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_lock()
|
||||
@@ -267,7 +308,7 @@ file_lock()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# file_unlock
|
||||
# File Unlock
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
file_unlock()
|
||||
@@ -290,7 +331,7 @@ file_unlock()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_set
|
||||
# Log Set
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_set()
|
||||
@@ -301,14 +342,14 @@ log_set()
|
||||
local log_trace="$4"
|
||||
|
||||
|
||||
LOG_FILE="${log_file}"
|
||||
LOG_LOCK="${lock_file}"
|
||||
LOG_ECHO="${echo_function}"
|
||||
LOG_FILE="${log_file}"
|
||||
LOG_LOCK="${lock_file}"
|
||||
LOG_ECHO="${echo_function}"
|
||||
|
||||
if [[ ${log_trace} != "" ]]
|
||||
then
|
||||
LOG_TRACE="${log_trace}"
|
||||
fi
|
||||
if [[ ${log_trace} != "" ]]
|
||||
then
|
||||
LOG_TRACE="${log_trace}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -316,7 +357,7 @@ log_set()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_print
|
||||
# Log Print
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_print()
|
||||
@@ -356,7 +397,7 @@ log_print()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_trace
|
||||
# Log Trace
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_trace()
|
||||
@@ -369,7 +410,7 @@ log_trace()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_info
|
||||
# Log Info
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_info()
|
||||
@@ -382,7 +423,7 @@ log_info()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_warning
|
||||
# Log Warning
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_warning()
|
||||
@@ -395,7 +436,7 @@ log_warning()
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
# log_error
|
||||
# Log Error
|
||||
#--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
log_error()
|
||||
|
||||
Reference in New Issue
Block a user