scripts/awk
author František Kučera <franta-hg@frantovo.cz>
Tue, 30 Jul 2019 23:56:31 +0200
branchv_0
changeset 34 a9d15976de90
parent 29 b3d1a671315b
child 35 eafffeea6a3e
permissions -rwxr-xr-x
Added tag v0.13 for changeset 5288af2e4921
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, either version 3 of the License, or
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
# (at your option) any later version.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
#
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# This program is distributed in the hope that it will be useful,
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
# GNU General Public License for more details.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
#
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# You should have received a copy of the GNU General Public License
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
# This is a simple helper script (/usr/bin/awk wrapper) for debugging the relpipe-tr-awk.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
# Captures STDIO and ARGS to log files.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
# Add this directory to the $PATH in order to use this wrapper script.
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
# Notes:
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#  - 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).
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#  - 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
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
logDir="/tmp/relpipe-tr-awk/";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
mkdir -p "$logDir";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
formatArgsTxt() {
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	for a in "$@"; do
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		echo -e "\e[32m>>> arg >>>\e[39m"; # or use lolcat
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		echo "$a";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		echo -e "\e[32m<<< arg <<<\e[39m";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	done
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
}
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
formatArgsTabular() {
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	i=0;
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	for a in "$@"; do
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		echo "$a" | while read a; do
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
			printf '%s\x0%s\x0' "$i" "$a";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		done;
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		((i++));
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	done | relpipe-in-cli generate-from-stdin args 2 i integer value string | relpipe-out-tabular
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
}
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
27
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    50
realAWK() {
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    51
	/usr/bin/awk "$@"
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    52
	echo "$?" > "$logDir/awk-status.log"
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    53
}
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    54
16
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
formatArgsTxt "$@" > "$logDir/awk-args.log";
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
27
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    57
tee "$logDir/awk-stdin.log" | realAWK "$@" 2> "$logDir/awk-stderr.log" | tee "$logDir/awk-stdout.log";
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    58
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    59
cat "$logDir/awk-stderr.log" >&2;
86d8bbc99e7b check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    60
exit `cat "$logDir/awk-status.log"`;
16
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
# Use e.g. GNU Screen with four windows to watch files:
29
b3d1a671315b AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    63
# tabs 2
16
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
# watchFile() { while inotifywait "$1" &>/dev/null ; do clear; cat "$1" ; done; }
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
# watchFile "/tmp/relpipe-tr-awk/awk-args.log"
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
# watchFile "/tmp/relpipe-tr-awk/awk-stdin.log"
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
# watchFile "/tmp/relpipe-tr-awk/awk-stdout.log"
4e8fe4cc70f0 awk wrapper script for debugging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
# watchFile "/tmp/relpipe-tr-awk/awk-stderr.log"