scripts/awk
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 22:01:17 +0200
branchv_0
changeset 35 eafffeea6a3e
parent 29 b3d1a671315b
permissions -rwxr-xr-x
fix license version: GNU GPLv3

#!/bin/bash

# Relational pipes
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# This is a simple helper script (/usr/bin/awk wrapper) for debugging the relpipe-tr-awk.
# Captures STDIO and ARGS to log files.
# Add this directory to the $PATH in order to use this wrapper script.
#
# Notes:
#  - if multiple relations are processed, only the last one will stay in the log files (can be solved by adding PID to the log path).
#  - if no relations are processed by the AWK sub-process, the logs will stay unchanged from the last run, although relpipe-tr-awk was executed

logDir="/tmp/relpipe-tr-awk/";

mkdir -p "$logDir";

formatArgsTxt() {
	for a in "$@"; do
		echo -e "\e[32m>>> arg >>>\e[39m"; # or use lolcat
		echo "$a";
		echo -e "\e[32m<<< arg <<<\e[39m";
	done
}

formatArgsTabular() {
	i=0;
	for a in "$@"; do
		echo "$a" | while read a; do
			printf '%s\x0%s\x0' "$i" "$a";
		done;
		((i++));
	done | relpipe-in-cli generate-from-stdin args 2 i integer value string | relpipe-out-tabular
}

realAWK() {
	/usr/bin/awk "$@"
	echo "$?" > "$logDir/awk-status.log"
}

formatArgsTxt "$@" > "$logDir/awk-args.log";

tee "$logDir/awk-stdin.log" | realAWK "$@" 2> "$logDir/awk-stderr.log" | tee "$logDir/awk-stdout.log";

cat "$logDir/awk-stderr.log" >&2;
exit `cat "$logDir/awk-status.log"`;

# Use e.g. GNU Screen with four windows to watch files:
# tabs 2
# watchFile() { while inotifywait "$1" &>/dev/null ; do clear; cat "$1" ; done; }
# watchFile "/tmp/relpipe-tr-awk/awk-args.log"
# watchFile "/tmp/relpipe-tr-awk/awk-stdin.log"
# watchFile "/tmp/relpipe-tr-awk/awk-stdout.log"
# watchFile "/tmp/relpipe-tr-awk/awk-stderr.log"