- 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:
@@ -1,4 +1,26 @@
|
||||
#!/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
|
||||
@@ -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} V${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()
|
||||
@@ -1,163 +0,0 @@
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
# ISL: Image Stack Log
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
# Global Variable
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
export ISL_FILE=/etc/img_stack_log
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ISL Add
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
isl_add()
|
||||
{
|
||||
url="$1"
|
||||
|
||||
|
||||
reg=${url%%/*}
|
||||
|
||||
if [[ "${reg}" == *.* ]]
|
||||
then
|
||||
url=${url#*/}
|
||||
else
|
||||
reg="-"
|
||||
fi
|
||||
|
||||
tag=${url/*:}
|
||||
name=${url%:*}
|
||||
|
||||
if [[ "${tag}" == "${name}" ]]
|
||||
then
|
||||
echo "Bad tag format in URL!"
|
||||
return 1
|
||||
fi
|
||||
|
||||
ts=$(date -u +"%Y/%m/%d %H:%M:%S")
|
||||
|
||||
if [[ ! -e ${ISL_FILE} ]]
|
||||
then
|
||||
id=1
|
||||
else
|
||||
id=$(( $(wc -l <${ISL_FILE}) + 1))
|
||||
fi
|
||||
|
||||
str="${id} ${ts} ${reg} ${name} ${tag}"
|
||||
|
||||
if [[ $id == "1" ]]
|
||||
then
|
||||
echo "${str}" >${ISL_FILE}
|
||||
else
|
||||
sed -i '1i\'"${str}" ${ISL_FILE}
|
||||
fi
|
||||
}
|
||||
|
||||
export -f isl_add
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ISL Top
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
isl_top()
|
||||
{
|
||||
line=$( head -1 ${ISL_FILE})
|
||||
|
||||
if [[ "$1" == "-i" ]]
|
||||
then
|
||||
set ${line}
|
||||
|
||||
echo "$5:$6"
|
||||
else
|
||||
echo "${line}"
|
||||
fi
|
||||
}
|
||||
|
||||
export -f isl_top
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ISL Cat
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
isl_cat()
|
||||
{
|
||||
if [[ "$1" == "-r" ]]
|
||||
then
|
||||
tac ${ISL_FILE}
|
||||
else
|
||||
cat ${ISL_FILE}
|
||||
fi
|
||||
}
|
||||
|
||||
export -f isl_cat
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ISL HTML Dump
|
||||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
isl_html_dump()
|
||||
{
|
||||
r_flag=""
|
||||
i_flag=""
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
if [[ "$1" == "-r" ]]
|
||||
then
|
||||
r_flag="$1"
|
||||
else
|
||||
if [[ "$1" == "-i" ]]
|
||||
then
|
||||
i_flag="-i"
|
||||
fi
|
||||
fi
|
||||
|
||||
shift
|
||||
done
|
||||
|
||||
i=1
|
||||
|
||||
isl_cat "${r_flag}" | while read line
|
||||
do
|
||||
set $line
|
||||
|
||||
if [[ $(( $i % 2)) -eq 0 ]]
|
||||
then
|
||||
print "${i_flag}" ' <tr class="shade">'
|
||||
else
|
||||
print "${i_flag}" " <tr>"
|
||||
fi
|
||||
|
||||
j=1
|
||||
while [[ $j -lt 7 ]]
|
||||
do
|
||||
if [[ "$j" == "1" ]]
|
||||
then
|
||||
print "${i_flag}" " <th>${!j}</th>"
|
||||
else
|
||||
print "${i_flag}" " <td>${!j}</td>"
|
||||
fi
|
||||
|
||||
j=$(( $j + 1))
|
||||
done
|
||||
|
||||
print "${i_flag}" " </tr>"
|
||||
i=$(( $i + 1))
|
||||
done
|
||||
}
|
||||
|
||||
export -f isl_html_dump
|
||||
Reference in New Issue
Block a user