Compare commits

...

31 Commits

Author SHA1 Message Date
olli
cb67db2a78 added headers 2024-10-22 10:55:58 +02:00
olli
71cf1ec44c change expionential numbers in normal notation in given file 2024-10-21 10:39:34 +02:00
olli
ed19ae5c0d too many arguments 2024-09-21 20:02:46 +02:00
olli
c4d9e053ee ignore VirtualBox 2024-09-08 17:43:19 +02:00
olli
8d88743624 super array v/vr 2024-08-24 13:16:37 +02:00
olli
2cdab60efb super array v/vr 2024-08-24 12:58:12 +02:00
olli
c761fe5d93 calc median g_ 2024-08-16 23:35:21 +02:00
olli
4342f0a5f8 calc median g_ 2024-08-16 23:27:37 +02:00
olli
be7e5da172 calc median g_ 2024-08-16 23:17:33 +02:00
olli
4174b3f7e3 calc median g_ 2024-08-16 23:06:35 +02:00
olli
9d5f5eac9f calc median g_ 2024-08-16 22:51:45 +02:00
olli
4f2f11bd1a calc median g_ 2024-08-16 22:42:16 +02:00
olli
d61329011b calc median 2024-08-16 17:27:41 +02:00
olli
4292b8e4dc var-name-fix 2024-07-17 12:00:04 +02:00
olli
5c930248eb var-name-fix 2024-07-08 11:09:52 +02:00
olli
9bc3710c32 fixes 2024-07-03 15:22:05 +02:00
olli
287a1fd2d3 fixes 2024-07-03 14:27:13 +02:00
olli
aac91e9a55 fixes 2024-07-02 22:39:02 +02:00
olli
fd8ad9799b fixes 2024-07-02 21:35:33 +02:00
olli
3c42aff8f5 fixes 2024-07-02 17:45:12 +02:00
olli
26460cf7f5 fixes 2024-06-28 12:10:19 +02:00
olli
8a3654e42b fixes 2024-06-28 11:55:25 +02:00
olli
855352bdf4 fixes 2024-06-28 11:24:11 +02:00
olli
f5af07290e fixes 2024-06-27 15:54:32 +02:00
olli
f0d521b49a fixes 2024-06-27 10:24:14 +02:00
olli
4d0bf673a5 fixes 2024-06-26 21:17:24 +02:00
olli
bf5c616f01 fixes 2024-06-26 19:00:38 +02:00
olli
0f7cddd5a6 fixes 2024-06-26 11:43:04 +02:00
olli
1e5e310fc8 fixes 2024-06-26 11:10:27 +02:00
olli
05475361e8 fixes 2024-06-25 12:11:46 +02:00
olli
ae1379180c fixes 2024-06-25 12:10:19 +02:00
11 changed files with 172 additions and 71 deletions

View File

@@ -2,10 +2,6 @@
. /etc/profile . /etc/profile
# export all functions
set -a
## Include functions ## Include functions
for bashfunc in $(find /etc/bash/gaboshlib -type f -name "g_*.bashfunc" -o -name "g_*.sh") for bashfunc in $(find /etc/bash/gaboshlib -type f -name "g_*.bashfunc" -o -name "g_*.sh")
do do
@@ -34,7 +30,7 @@ g_tmp="$g_tmp/g_$g_scriptname-$$"
# START and EXIT Notification # START and EXIT Notification
g_trap_exit="g_logger EXITING $g_scriptname ; rm -r $g_tmp ; g_kill_all_background_jobs >/dev/null 2>&1" g_trap_exit="g_logger EXITING $g_scriptname ; rm -r $g_tmp ; g_kill_all_background_jobs >/dev/null 2>&1"
trap "$g_trap_exit" EXIT trap "$g_trap_exit" INT TERM EXIT
g_syslogtag="g_bash-script:$g_scriptname[$$]" g_syslogtag="g_bash-script:$g_scriptname[$$]"
[ $g_scriptname = "bash" ] || g_logger STARTING $g_scriptname [ $g_scriptname = "bash" ] || g_logger STARTING $g_scriptname

View File

@@ -72,6 +72,7 @@ vpn
.config/Nextcloud/ .config/Nextcloud/
nextcloud-test.*/ nextcloud-test.*/
.VirtualBox/ .VirtualBox/
VirtualBox/
DMS/ DMS/
tmp tmp
.zoom/ .zoom/

View File

@@ -1,5 +1,5 @@
function g_basename { function g_basename {
g_basename_result="${1##*/}" g_basename_result=${1##*/}
g_basename_result="${g_basename_result:-/}" g_basename_result=${g_basename_result:-/}
} }

View File

@@ -1,66 +1,58 @@
function g_calc { function g_calc {
# function for calculating via bc running in the background (much faster then starting bc every time)
# $g_fd_bc_in $fd_bc_out have to be global
unset g_calc_result unset g_calc_result
local g_calc_jobs g_rnd local g_jobs
local g_bc_running=true mapfile -t g_jobs < <(jobs -r)
mapfile -t g_calc_jobs < <(jobs -r)
g_calc_jobs_v=${g_calc_jobs[*]}
[[ $g_calc_jobs_v =~ bc ]] || unset g_bc_running
# Use bc in backround for multiple bc's running much faster # check if bc is already running
if [ -z "${g_bc_running}" ] if [[ -z "$g_fd_bc_in" || -z "$g_fd_bc_out" || ${g_jobs[*]} != *bc* ]]
then then
local bc_input="$g_tmp/$$-g_bc_input"
g_kill_all_background_jobs bc-ql local bc_output="$g_tmp/$$-g_bc_output"
g_kill_all_background_jobs sed # create fifo pipe
[ -p "$bc_input" ] || mkfifo "$bc_input"
g_rnd=$$ [ -p "$bc_output" ] || mkfifo "$bc_output"
mkdir -p ${g_tmp}/${g_rnd} # run bc in background und switch i/o to pipes
[ -p ${g_tmp}/${g_rnd}/bc-in ] || mkfifo ${g_tmp}/${g_rnd}/bc-in bc -ql < "$bc_input" > "$bc_output" 2>&1 &
[ -p ${g_tmp}/${g_rnd}/bc-out ] || mkfifo ${g_tmp}/${g_rnd}/bc-out # store in filedescriptiors
[ -p ${g_tmp}/${g_rnd}/bc-sed-in ] || mkfifo ${g_tmp}/${g_rnd}/bc-sed-in exec {g_fd_bc_in}> "$bc_input"
[ -p ${g_tmp}/${g_rnd}/bc-sed-out ] || mkfifo ${g_tmp}/${g_rnd}/bc-sed-out exec {g_fd_bc_out}< "$bc_output"
# bc stream channel
{ bc -ql <${g_tmp}/${g_rnd}/bc-in 2>&1 >${g_tmp}/${g_rnd}/bc-out & } 2>/dev/null
exec 3>${g_tmp}/${g_rnd}/bc-in 4<${g_tmp}/${g_rnd}/bc-out
# sed stream channel
{ sed -su 's/^\./0./; s/^-\./-0./' <${g_tmp}/${g_rnd}/bc-sed-in >${g_tmp}/${g_rnd}/bc-sed-out & } 2>/dev/null
exec 5>${g_tmp}/${g_rnd}/bc-sed-in 6<${g_tmp}/${g_rnd}/bc-sed-out
g_bc_running="true"
fi fi
# send calculation and read result
echo "$1" >&${g_fd_bc_in}
read -u ${g_fd_bc_out} g_calc_result
# do bc # check if there is a output
echo "scale=8; $@" >&3 if [ -z "$g_calc_result" ]
local g_bc_out
if ! read -t 0.1 g_bc_out <&4
then then
g_traceback "$@" echo "${FUNCNAME} $@" 1>&2
( exec 3<&- ) 2>/dev/null unset g_calc_result
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
#unset g_bc_running
return 1 return 1
fi fi
# do sed # fix bc output -> change for example .224 to 0.224 and -.224 to -0.224
echo ${g_bc_out} >&5 [[ $g_calc_result == "."* ]] && g_calc_result="0$g_calc_result"
local g_sed_out [[ $g_calc_result == "-."* ]] && g_calc_result="-0.${g_calc_result#-.}"
if ! read -t 0.1 g_sed_out <&6
# remove ending 0 if for exabple 4.54300000
while [[ $g_calc_result =~ [.] && ${g_calc_result: -1} == "0" ]]
do
g_calc_result=${g_calc_result: : -1}
done
# remove ending . for example "100." -> 100
[[ $g_calc_result == *"." ]] && g_calc_result=${g_calc_result%?}
# check output
if ! g_num_valid_number "$g_calc_result"
then then
g_traceback "$@" echo "${FUNCNAME} $@" 1>&2
( exec 3<&- ) 2>/dev/null unset g_calc_result
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
#unset g_bc_running
return 1 return 1
fi fi
# store result
g_calc_result=$g_sed_out
} }

View File

@@ -8,13 +8,12 @@ function g_kill_all_background_jobs {
then then
kill -9 ${g_pids[*]} >/dev/null 2>&1 kill -9 ${g_pids[*]} >/dev/null 2>&1
else else
local g_cmdline=$1 local g_cmdline="$1"
local g_proc local g_proc
local g_pid local g_pid
for g_pid in "${g_pids[@]}" for g_pid in "${g_pids[@]}"
do do
echo $g_pid read g_proc < <(tr "\0" " " < /proc/${g_pid}/cmdline)
readarray g_proc < "/proc/$g_pid/cmdline"
[ "$g_proc" = "$g_cmdline" ] && kill -9 $g_pid >/dev/null 2>&1 [ "$g_proc" = "$g_cmdline" ] && kill -9 $g_pid >/dev/null 2>&1
done done
fi fi

26
gaboshlib/g_median.sh Normal file
View File

@@ -0,0 +1,26 @@
function g_median {
unset g_median_result
# Array with numbers
local g_numbers=("$@")
# sort array
local g_sorted_numbers=($(printf "%s\n" "${g_numbers[@]}" | sort -n))
# number of elements
local g_num_elements=${#g_sorted_numbers[@]}
# calculate the middle
local g_middle=$(($g_num_elements/2))
# even/odd number
if (($g_num_elements % 2 == 1))
then
# odd number
g_median_result="${g_sorted_numbers[$g_middle]}"
else
# even number
g_calc "(${g_sorted_numbers[$g_middle - 1]} + ${g_sorted_numbers[$g_middle]}) / 2"
g_median_result="$g_calc_result"
fi
}

View File

@@ -5,16 +5,16 @@ function g_num_exponential2normal {
if [[ "$1" =~ e-[0-9] ]] if [[ "$1" =~ e-[0-9] ]]
then then
# convert # convert
printf -v f_g_num_exponential2normal_result -- "%.12f" "$1" printf -v g_num_exponential2normal_result -- "%.12f" "$1"
# remove ending 0 # remove ending 0
if [[ $f_g_num_exponential2normal_result =~ \. ]] if [[ $g_num_exponential2normal_result =~ \. ]]
then then
f_g_num_exponential2normal_result=${f_g_num_exponential2normal_result%%+(0)} g_num_exponential2normal_result=${g_num_exponential2normal_result%%+(0)}
f_g_num_exponential2normal_result=${f_g_num_exponential2normal_result%%.} g_num_exponential2normal_result=${g_num_exponential2normal_result%%.}
fi fi
return 0 return 0
else else
f_g_num_exponential2normal_result=$1 g_num_exponential2normal_result=$1
return 2 return 2
fi fi
} }

View File

@@ -0,0 +1,11 @@
function g_num_exponential2normal_file {
# changes expionential numbers in normal notation in given file
local g_file=$1
local g_substitution g_exnum
for g_exnum in $(egrep -o -i "[0-9]+\.[0-9]+e[\+\-][0-9]+" "$g_file" | sort -u)
do
g_num_exponential2normal $g_exnum && g_substitution="${g_substitution}s/${g_exnum}/${g_num_exponential2normal_result}/g;"
done
sed -i "${g_substitution}" "$g_file" && return 0
}

View File

@@ -1,5 +1,62 @@
function g_python { function g_python {
# function for running python in the background
# $g_fd_python_in $g_fd_python_out have to be global
unset g_python_result
local g_jobs
mapfile -t g_jobs < <(jobs -r)
# check if python is already running
if [[ -z $g_fd_python_in || -z $g_fd_python_out || ${g_jobs[*]} != *python3* ]]
then
local python_input="$g_tmp/$$-g_python_input"
local python_output="$g_tmp/$$-g_python_output"
local python_error="$g_tmp/$$-g_python_error"
# create fifo pipe
[ -p "$python_input" ] || mkfifo "$python_input"
[ -p "$python_output" ] || mkfifo "$python_output"
[ -p "$python_error" ] || mkfifo "$python_error"
# run bc in background und switch i/o to pipes
python3 -iuq < "$python_input" > "$python_output" 2> "$python_error" &
# store in filedescriptiors
exec {g_fd_python_in}> "$python_input"
exec {g_fd_python_out}< "$python_output"
exec {g_fd_python_error}< "$python_error"
fi
# read potential old output
read -t 0.001 -u ${g_fd_python_out} g_python_result_old
# send python command
echo "$@
print('')" >&${g_fd_python_in}
# read output
read -u ${g_fd_python_out} g_python_result
local g_errline
local g_err_msg
# look for error
while read -t 0.001 -u ${g_fd_python_error} g_errline
do
[[ "$g_errline" =~ \>\>\>$ ]] && break
[ -z "$g_errline" ] && break
g_err_msg="$g_err_msg
$g_errline"
done
if [ -n "$g_err_msg" ]
then
g_echo_error "$g_err_msg"
return 1
fi
}
function g_python_old {
local g_python_tmp=${g_tmp}/$$ local g_python_tmp=${g_tmp}/$$
g_python_out=${g_python_tmp}/python-out g_python_out=${g_python_tmp}/python-out
local g_python_in=${g_python_tmp}/python-in local g_python_in=${g_python_tmp}/python-in

View File

@@ -8,7 +8,7 @@ function g_read_csv {
local g_headline=$3 local g_headline=$3
local g_separator=$4 local g_separator=$4
local g_headline_item i l r local g_headline_item i l r g_csv_headline_array
# check for individual separator # check for individual separator
[ -z "$g_separator" ] && g_separator="," [ -z "$g_separator" ] && g_separator=","
@@ -39,14 +39,20 @@ function g_read_csv {
[ -z "$g_headline" ] && g_headline=$(head -n1 "${g_csvfile}") [ -z "$g_headline" ] && g_headline=$(head -n1 "${g_csvfile}")
# read the headline # read the headline
unset g_csv_headline_array_ref
g_array "$g_headline" g_csv_headline_array_ref $g_separator g_array "$g_headline" g_csv_headline_array_ref $g_separator
# prepare varnames from headline(s) # prepare varnames from headline(s)
for g_headline_item in "${g_csv_headline_array_ref[@]}" for g_headline_item in "${g_csv_headline_array_ref[@]}"
do do
g_csv_headline_array+=("${g_headline_item//[^a-zA-Z0-9]/}") g_csv_headline_array+=("${g_headline_item//[^a-zA-Z0-9_]/}")
done done
g_basename $g_csvfile
local g_csvfile_base=${g_basename_result/\.history*.csv/}
g_csvfile_base=${g_csvfile_base//[^a-zA-Z0-9_]/}
g_csvfile_base=${g_csvfile_base//ECONOMY*/}
# read last lines if defined or complete csv file # read last lines if defined or complete csv file
if [ -n "$g_last_lines" ] if [ -n "$g_last_lines" ]
then then
@@ -54,14 +60,15 @@ function g_read_csv {
g_csvfile="${g_tmp}/g_csv_tmp.csv" g_csvfile="${g_tmp}/g_csv_tmp.csv"
fi fi
# read csv file to array # read csv file to array
g_array "$g_csvfile" g_csv_array_ref g_array "$g_csvfile" g_csv_array_ref
g_csv_array=("${g_csv_array_ref[@]}") g_csv_array=("${g_csv_array_ref[@]}")
# go reverse through array # create associative arrays forward and reverse and superarray v/vr
declare -Ag v_csv_array_associative declare -Ag v_csv_array_associative
declare -Ag v_csv_array_associative_reverse declare -Ag v_csv_array_associative_reverse
declare -Ag v
declare -Ag vr
l=0 l=0
for (( r=${#g_csv_array[@]}-1 ; r>=0 ; r-- )) for (( r=${#g_csv_array[@]}-1 ; r>=0 ; r-- ))
do do
@@ -75,6 +82,14 @@ function g_read_csv {
[ "$l" = 0 ] && declare -g v_${g_headline_item}="${g_csv_line_array[i]}" [ "$l" = 0 ] && declare -g v_${g_headline_item}="${g_csv_line_array[i]}"
v_csv_array_associative[${g_headline_item}_${r}]="${g_csv_line_array[i]}" v_csv_array_associative[${g_headline_item}_${r}]="${g_csv_line_array[i]}"
v_csv_array_associative_reverse[${g_headline_item}_${l}]="${g_csv_line_array[i]}" v_csv_array_associative_reverse[${g_headline_item}_${l}]="${g_csv_line_array[i]}"
if [ -z "${g_csvfile_base}" ]
then
v[${g_headline_item}_${r}]=${g_csv_line_array[i]}
vr[${g_headline_item}_${l}]=${g_csv_line_array[i]}
else
v[${g_csvfile_base}_${g_headline_item}_${r}]=${g_csv_line_array[i]}
vr[${g_csvfile_base}_${g_headline_item}_${l}]=${g_csv_line_array[i]}
fi
((i++)) ((i++))
done done
((l++)) ((l++))

View File

@@ -1,3 +1,7 @@
function g_wget { function g_wget {
wget -T 10 -t 2 -U "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" $@ wget -T 10 -t 2 \
--header="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" \
--header="Content-Type: application/json" \
--header="Accept-Language: en-US,en," \
$@
} }