From: Steven Baltakatei Sandoval Date: Thu, 29 Jul 2021 13:17:56 +0000 (+0000) Subject: Merge branch 'master' into develop X-Git-Tag: 0.5.0~49^2 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/2e5e9a712d8baa108ba6ef680343a386138e2c80?hp=e6bb2ae13949291359b48d66847ae1605b160bc2 Merge branch 'master' into develop --- diff --git a/unitproc/bkbtcblockclock b/unitproc/bkbtcblockclock new file mode 100644 index 0000000..a7d6073 --- /dev/null +++ b/unitproc/bkbtcblockclock @@ -0,0 +1,217 @@ +#!/bin/bash +# Desc: Prints latest block from bitcoin-cli +# Usage: bkbtcblockclock +# Output: stdout: timestamped block numbers +# Depends: bitcoin-cli (from bitcoind), jq, gnu coreutils 8.30 +# Version: 0.0.1 + +#==BEGIN Define script parameters== +delay="1" +#===BEGIN Define variables=== +declare -Ag appRollCall # Associative array for storing app status +declare -Ag fileRollCall # Associative array for storing file status +declare -Ag dirRollCall # Associative array for storing dir status +#===END Define variables=== + + +#===BEGIN Declare local script functions=== +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +checkapp() { + # Desc: If arg is a command, save result in assoc array 'appRollCall' + # Usage: checkapp arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'appRollCall' + # Output: adds/updates key(value) to global assoc array 'appRollCall' + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command + appRollCall[$arg]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + appRollCall[$arg]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that app exists +checkfile() { + # Desc: If arg is a file path, save result in assoc array 'fileRollCall' + # Usage: checkfile arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'fileRollCall' + # Output: adds/updates key(value) to global assoc array 'fileRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -f "$arg" ]; then + fileRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + fileRollCall["$arg"]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that file exists +checkdir() { + # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' + # Usage: checkdir arg1 arg2 arg3 ... + # Version 0.1.1 + # Input: global assoc. array 'dirRollCall' + # Output: adds/updates key(value) to global assoc array 'dirRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: Bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -d "$arg" ]; then + dirRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi + else + dirRollCall["$arg"]="false"; returnState="false"; + fi + done + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi +} # Check that dir exists +displayMissing() { + # Desc: Displays missing apps, files, and dirs + # Usage: displayMissing + # Version 0.1.1 + # Input: associative arrays: appRollCall, fileRollCall, dirRollCall + # Output: stderr: messages indicating missing apps, file, or dirs + # Depends: bash 5, checkAppFileDir() + local missingApps value appMissing missingFiles fileMissing + local missingDirs dirMissing + + #==BEGIN Display errors== + #===BEGIN Display Missing Apps=== + missingApps="Missing apps :"; + #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done + for key in "${!appRollCall[@]}"; do + value="${appRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing apps: $key => $value"; + missingApps="$missingApps""$key "; + appMissing="true"; + fi; + done; + if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingApps" 1>&2; + fi; + unset value; + #===END Display Missing Apps=== + + #===BEGIN Display Missing Files=== + missingFiles="Missing files:"; + #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + for key in "${!fileRollCall[@]}"; do + value="${fileRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing files: $key => $value"; + missingFiles="$missingFiles""$key "; + fileMissing="true"; + fi; + done; + if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingFiles" 1>&2; + fi; + unset value; + #===END Display Missing Files=== + + #===BEGIN Display Missing Directories=== + missingDirs="Missing dirs:"; + #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + for key in "${!dirRollCall[@]}"; do + value="${dirRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing dirs: $key => $value"; + missingDirs="$missingDirs""$key "; + dirMissing="true"; + fi; + done; + if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. + echo "$missingDirs" 1>&2; + fi; + unset value; + #===END Display Missing Directories=== + + #==END Display errors== +} # Display missing apps, files, dirs +getBlockNum() { + # depends: yell(), die(), try() + blockNum="$(try bitcoin-cli getblockchaininfo | try jq '."headers"')" + echo "$blockNum"; +} +printNum() { + # depends: yell(), die(), try() + # usage printNum arg1 + echo "[$(date --iso-8601=seconds)]:$1" + } +main() { + # Depends: bitcoin-cli (from bitcoind) + # Depends: getBlockNum(), printNum() + + # Check dependencies + if ! checkapp bitcoin-cli jq date; then + yell "ERROR:Apps missing." >&2; + displayMissing; + exit 1; + fi; + + lastBlock="$(getBlockNum)"; + printNum "$lastBlock"; + #echo "$lastBlock"; + n=0; + while true; do + # update latestBlock + latestBlock="$(getBlockNum)"; + + # compare last and latestBlock + if [[ $latestBlock -gt $lastBlock ]]; then + ## true: new block + ## print block + printNum "$latestBlock"; + #echo "$(date --iso-8601=seconds)":"$latestBlock" + ## update lastBlock + lastBlock="$latestBlock" + : + else + ## no new block + sleep "$delay" + fi; + # update counter + ((n++)); + done; +} +#===END Declare local script functions=== +#==END Define script parameters== + +main "$@"; + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+ diff --git a/unitproc/bktemp-checkInt b/unitproc/bktemp-checkInt new file mode 100644 index 0000000..cfe396e --- /dev/null +++ b/unitproc/bktemp-checkInt @@ -0,0 +1,53 @@ +#!/bin/bash +# Desc: Checks if arg is an integer + +#==BEGIN Define script parameters== +#===BEGIN Declare local script functions=== +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +checkInt() { + # Desc: Checks if arg is integer + # Usage: checkInt arg + # Input: arg: integer + # Output: - return code 0 (if arg is integer) + # - return code 1 (if arg is not integer) + # Example: if ! checkInt $arg; then echo "not int"; fi; + # Version: 0.0.1 + local returnState + + #===Process Arg=== + if [[ $# -ne 1 ]]; then + die "ERROR:Invalid number of arguments:$#"; + fi; + + RETEST1='^[0-9]+$'; # Regular Expression to test + if [[ ! $1 =~ $RETEST1 ]] ; then + returnState="false"; + else + returnState="true"; + fi; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Checks if arg is integer + +#===END Declare local script functions=== +#==END Define script parameters== + +#==BEGIN test code== +if checkInt 4; then yell "success"; fi; +sleep 1; +if checkInt "foo"; then yell "success"; else yell "fail"; fi; +sleep 1; +if checkInt "foo" "bar" "baz" 1; then yell "success"; else yell "fail"; fi; +sleep 1; +if checkInt; then yell "success"; else yell "fail"; fi; +#==END test code== + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+