feat(unitproc/bkt-remove_leading_zeroes):Add bash function
[BK-2020-03.git] / user / space4
1 #!/usr/bin/env bash
2 # Desc: Adds blank line every 4 lines
3 # Usage: cat file | space4
4 # Example: seq 1 10 | space 4
5 # Version: 0.0.1
6 # Depends: Bash 5.1.16
7
8 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
9 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
10 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
11 read_stdin() {
12 # Desc: Consumes stdin; outputs as stdout lines
13 # Input: stdin (consumes)
14 # Output: stdout (newline delimited)
15 # Example: printf "foo\nbar\n" | read_stdin
16 # Depends: GNU bash (version 5.1.16)
17 # Version: 0.0.1
18 local input_stdin output;
19
20 # Store stdin
21 if [[ -p /dev/stdin ]]; then
22 input_stdin="$(cat -)";
23 fi;
24
25 # Store as output array elements
26 ## Read in stdin
27 if [[ -n $input_stdin ]]; then
28 while read -r line; do
29 output+=("$line");
30 done < <(printf "%s\n" "$input_stdin");
31 fi;
32
33 # Print to stdout
34 printf "%s\n" "${output[@]}";
35 }; # read stdin to stdout lines
36 main() {
37 # Depends: BK-2020-03: yell(), die(), must(), read_stdin()
38 if [[ $# -gt 0 ]]; then die "FATAL:This script uses no positional arguments.:$#"; fi;
39 n=0;
40 while read -r line; do
41 printf "%s\n" "$line";
42 if ! (( (n + 1) % 4 )); then
43 printf "\n";
44 fi;
45 ((n++));
46 done < <(read_stdin);
47 }; # main program
48
49 main "$@";
50
51 # Author: Steven Baltakatei Sandoval
52 # License: GPLv3+