Skip to content
Snippets Groups Projects
Commit dbeae529 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update ini files

parent bbc8bb6b
Branches
No related tags found
1 merge request!129Db Profiles
...@@ -48,8 +48,8 @@ SOURCE_DIR=/var/lib/mysql ...@@ -48,8 +48,8 @@ SOURCE_DIR=/var/lib/mysql
# it sets mysql_FOUND as flag # it sets mysql_FOUND as flag
function mysql._check(){ function mysql._check(){
# j_requireBinary "mysql" 1 j_requireBinary "mysql" 1
# j_requireBinary "mysqldump" 1 j_requireBinary "mysqldump" 1
# j_requireProcess "mysqld|mariadb" 1 # j_requireProcess "mysqld|mariadb" 1
# if [ ! -d $SOURCE_DIR ]; then # if [ ! -d $SOURCE_DIR ]; then
......
# ======================================================================
#
# LOCAL COUCHDB SERVER
#
# ======================================================================
[detect]
tcp = 5984
tcp-target = localhost
tcp-process = 'beam.smp'
[set]
su = ''
dbuser = 'admin'
dbpassword = 'HereIsMyAdminPassword'
params = ''
env = 'export COUCH_URL=http://{{dbuser}}:{{dbpassword}}@{{tcp-target}}:{{tcp-port}}/'
# ----------------------------------------------------------------------
# ---------------------------------------------------------------------- # ======================================================================
# what to detect #
# ---------------------------------------------------------------------- # LOCAL MYSQL INSTANCE
#
# ======================================================================
[detect] [detect]
# binaries that must befound, comma seperated
binary = 'mysql,mysqldump'
# a running process that must be found
process = 'mysqld|mariadb' process = 'mysqld|mariadb'
# a port that must be open on a given host
tcp = 3306 tcp = 3306
tcp-target = localhost tcp-target = localhost
# process that opens a port (see netstat -tulpen) - works for local services only
# "slirp4netns" is docker network stack
# "rootlesskit" is docker too
tcp-process = 'mysqld|mariadbd' tcp-process = 'mysqld|mariadbd'
# ----------------------------------------------------------------------
# data to apply if it was found
# ----------------------------------------------------------------------
[set] [set]
su = '' su = ''
dbuser = 'root' dbuser = ''
dbpassword = '12345678' dbpassword = ''
# unschön - das ist in der Prozessliste params = ''
params = '--port={{tcp}} --password={{dbpassword}} --user={{dbuser}} --host={{tcp-target}}' env = ''
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# ======================================================================
#
# LOCAL SQLITE DATABASES
#
# ======================================================================
[detect]
# ----------------------------------------------------------------------
# what to detect
# ----------------------------------------------------------------------
# file[] = "/var/www/database/logs.db"
type = "sqlite"
[set]
su = ''
dbuser = ''
dbpassword = ''
params = ''
env = ''
# ----------------------------------------------------------------------
...@@ -4,11 +4,18 @@ ...@@ -4,11 +4,18 @@
# READ INI FILE with Bash # READ INI FILE with Bash
# https://axel-hahn.de/blog/2018/06/08/bash-ini-dateien-parsen-lesen/ # https://axel-hahn.de/blog/2018/06/08/bash-ini-dateien-parsen-lesen/
# #
# Author: Axel hahn
# License: GNU GPL 3.0
# Source: https://github.com/axelhahn/bash_iniparser
# Docs: https://www.axel-hahn.de/docs/bash_iniparser/
#
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2024-02-04 v0.1 Initial version # 2024-02-04 v0.1 Initial version
# 2024-02-08 v0.2 add ini.varexport; improve replacements of quotes # 2024-02-08 v0.2 add ini.varexport; improve replacements of quotes
# 2024-02-10 v0.3 handle spaces and tabs around vars and values # 2024-02-10 v0.3 handle spaces and tabs around vars and values
# 2024-02-12 v0.4 rename local varables # 2024-02-12 v0.4 rename local varables
# 2024-02-20 v0.5 handle special chars in keys; add ini.dump + ini.help
# 2024-02-21 v0.6 harden ini.value for keys with special chars; fix fetching last value
# ====================================================================== # ======================================================================
INI_FILE= INI_FILE=
...@@ -25,7 +32,7 @@ function ini.set(){ ...@@ -25,7 +32,7 @@ function ini.set(){
INI_FILE= INI_FILE=
INI_SECTION= INI_SECTION=
if [ ! -f "$1" ]; then if [ ! -f "$1" ]; then
echo "ERROR: file does not exists: $1" echo "ERROR: file does not exist: $1"
exit 1 exit 1
fi fi
INI_FILE="$1" INI_FILE="$1"
...@@ -101,12 +108,16 @@ function ini.value(){ ...@@ -101,12 +108,16 @@ function ini.value(){
local myinisection=$2 local myinisection=$2
local myvarname=$3 local myvarname=$3
local out local out
regex="${myvarname//[^a-zA-Z0-9:()]/.}"
out=$(ini.section "${myinifile}" "${myinisection}" \ out=$(ini.section "${myinifile}" "${myinisection}" \
| grep "^[\ \t]*${myvarname}[\ \t]*=.*" \ | sed "s,^[\ \t]*,,g" \
| sed "s,[\ \t]*=,=,g" \
| grep -F "${myvarname}=" \
| grep "^${regex}=" \
| cut -f 2- -d "=" \ | cut -f 2- -d "=" \
| sed -e 's,^\ *,,' -e 's, *$,,' | sed -e 's,^\ *,,' -e 's, *$,,'
) )
grep "\[\]$" <<< "myvarname" >/dev/null && out="echo $out | tail -1" grep "\[\]$" <<< "$myvarname" >/dev/null || out="$( echo "$out" | tail -1 )"
# delete quote chars on start and end # delete quote chars on start and end
grep '^".*"$' <<< "$out" >/dev/null && out=$(echo "$out" | sed -e's,^"\(.*\)"$,\1,g') grep '^".*"$' <<< "$out" >/dev/null && out=$(echo "$out" | sed -e's,^"\(.*\)"$,\1,g')
...@@ -115,6 +126,99 @@ function ini.value(){ ...@@ -115,6 +126,99 @@ function ini.value(){
fi fi
} }
# dump the ini file for visuall check of the parsing functions
# param string filename
ini.dump() {
local myinifile=${1:-$INI_FILE}
echo -en "\e[1;33m"
echo "+----------------------------------------"
echo "|"
echo "| $myinifile"
echo "|"
echo -e "+----------------------------------------\e[0m"
echo -e "\e[34m"
cat "$myinifile" | sed "s,^, ,g"
echo -e "\e[0m"
echo " Parsed data:"
echo
ini.sections "$myinifile" | while read -r myinisection; do
echo -e " --+-- section \e[35m[$myinisection]\e[0m"
echo " |"
ini.keys "$myinifile" "$myinisection" | while read -r mykey; do
value="$(ini.value "$myinifile" "$myinisection" "$mykey")"
# printf " %-15s => %s\n" "$mykey" "$value"
printf " \`---- %-20s => " "$mykey"
echo -e "\e[1;36m$value\e[0m"
done
echo
done
echo
}
function ini.help(){
cat <<EOH
INI.CLASS.SH
A bash implementation to read ini files.
Author: Axel hahn
License: GNU GPL 3.0
Source: https://github.com/axelhahn/bash_iniparser
Docs: https://www.axel-hahn.de/docs/bash_iniparser/
Usage:
(1)
source the file ini.class.sh
(2)
ini.help
to show this help with all available functions.
BASIC ACCESS:
ini.value <FILE> <SECTION> <KEY>
Get a avlue of a variable in a given section.
Tho shorten ini.value with 3 parameters:
ini.set <FILE> [<SECTION>]
or
ini.set <FILE>
ini.setsection <SECTION>
This sets the ini file and/ or section as default.
Afterwards you can use:
ini.value <KEY>
and
ini.value <SECTION> <KEY>
OTHER GETTERS:
ini.sections <FILE>
Get all sections in the ini file.
The <FILE> is not needed if ini.set <FILE> was used before.
ini.keys <FILE> <SECTION>
Get all keys in the given section.
The <FILE> is not needed if ini.set <FILE> was used before.
The <SECTION> is not needed if ini.setsection <SECTION> was used
before.
ini.dump <FILE>
Get a nice overview of the ini file.
You get a colored view of the content and a parsed view of the
sections and keys + values.
EOH
}
# Create bash code to export all variables as hash. # Create bash code to export all variables as hash.
# Example: eval "$( ini.varexport "cfg_" "$inifile" )" # Example: eval "$( ini.varexport "cfg_" "$inifile" )"
# #
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment