From 576c391575303a7b37c1d8ae411f9e5ae6ce8c15 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Tue, 5 May 2020 21:34:48 +0200 Subject: [PATCH] Added: Possibilty to run date through background fifo for bash <5 --- bashtop | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/bashtop b/bashtop index 28410e1..785c61c 100755 --- a/bashtop +++ b/bashtop @@ -200,16 +200,36 @@ if [[ -n $EPOCHREALTIME ]]; then ms_out=$((${EPOCHREALTIME/[.,]/}/1000)) } -#* If not, use date command +#* If not, use date command, through fifo if possible else - get_ms() { #? Set given variable to current epoch millisecond with date command - local count - local -n ms_out=$1 - ms_out="" - while [[ -z $ms_out ]] && ((++count<=10)); do + tmpdir="" + if [[ -n $XDG_RUNTIME_DIR && -w "$XDG_RUNTIME_DIR" ]]; then + tmpdir="$XDG_RUNTIME_DIR" + elif [[ -w /dev/shm ]]; then + tmpdir="/dev/shm" + elif [[ -w /tmp ]]; then + tmpdir="/tmp" + fi + + if [[ -n $tmpdir ]] && command -v stdbuf >/dev/null 2>&1; then + mkfifo "${tmpdir}/bashtop_datefifo" + exec 5> >(exec stdbuf -o0 date -f - +%s%3N > "${tmpdir}/bashtop_datefifo" 2>&1) + exec 6< "${tmpdir}/bashtop_datefifo" + rm "${tmpdir}/bashtop_datefifo" + + get_ms() { #? Set given variable to current epoch millisecond with date command through background fifo + local -n ms_out=$1 + echo now >&5 && + read -u 6 ms_out + } + + else + get_ms() { #? Set given variable to current epoch millisecond with forked date command + local -n ms_out=$1 + ms_out="" read ms_out < <(date +%s%3N) - done - } + } + fi fi init_() { #? Collect needed information and set options before startig main loop