From: Steven Baltakatei Sandoval Date: Fri, 13 Jan 2023 04:10:14 +0000 (+0000) Subject: Merge branch 'develop' X-Git-Tag: 0.7.0^0 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/6d44d2ad506f4107fd410ef4be6f93b8c5a9dfbb?hp=9d839273a1a4a5b8cf6153485ed788897ae6b0b4 Merge branch 'develop' --- diff --git a/unitproc/bkt-read_stdin_psarg b/unitproc/bkt-read_stdin_psarg new file mode 100644 index 0000000..50f76a7 --- /dev/null +++ b/unitproc/bkt-read_stdin_psarg @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +read_stdin_psarg() { + # Desc: Consumes stdin and reads arguments; outputs as stdout lines + # Input: stdin (consumes) + # Input: args + # Output: stdout (newline delimited) + # Example: read_stdin_psarg "$@" + # Depends: GNU bash (version 5.1.16) + local input_stdin input_psarg output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)"; + fi; + yell "DEBUG:$(declare -p input_stdin)"; + + # Store arguments + if [[ $# -gt 0 ]]; then + input_psarg="$@"; + fi; + yell "DEBUG:$(declare -p input_psarg)"; + + # Combine as output array elements + ## Read in stdin + if [[ -n $input_stdin ]]; then + while read -r line; do + output+=("$line"); + done < <(printf "%s\n" "$input_stdin"); + fi; + ## Read in positional arguments + if [[ -n $input_psarg ]]; then + for arg in "$@"; do + output+=("$arg"); + done; + fi; + yell "DEBUG:$(declare -p output)"; + + # Print to stdout + printf "%s\n" "${output[@]}"; +}; # read stdin and positional argument to stdout lines + +main() { + read_stdin_psarg "$@"; +}; + +main "$@"; diff --git a/user/bkdatediff b/user/bkdatediff new file mode 100644 index 0000000..8a669ab --- /dev/null +++ b/user/bkdatediff @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Desc: Get time difference two between date-parsable strings in seconds +# Usage: bkdatediff [STRING] [STRING] +# Example: bkdatediff "2023-01-10T08:36:42+00" "2023-01-10T12:30:13+00" +# Version 0.0.1 + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +check_depends() { + if ! command -v date 1>/dev/random 2>&1; then die "FATAL:Missing:date"; fi; + return 0; +}; # check dependencies +show_usage() { + cat <<'EOF' + USAGE: + bkdatediff [STRING1] [STRING2] + + STRING1, STRING2 + `date` parsable strings. (i.e. variable STR in `date --date='$STR'`) + + EXAMPLE: + bkdatediff "2023-01-10T08:36:42+00" "2023-01-10T12:30:13+00" +EOF +}; +main() { + local t1 t2; + check_depends; + # check args + if [[ ! $# -eq 2 ]]; then show_usage; die "FATAL:Wrong number of args:$#"; fi; + + # get unix epoch + t1="$(must date --date="$1" +%s)"; + t2="$(must date --date="$2" +%s)"; + + # print diff in seconds + printf "%s\n" "$((t2 - t1))"; +}; # main program + +main "$@";