From 2ddc75a555b0be46a3b4f94b5e9a8e44aa960c83 Mon Sep 17 00:00:00 2001
From: Steven Baltakatei Sandoval <baltakatei@gmail.com>
Date: Tue, 20 Feb 2024 21:40:00 +0000
Subject: [PATCH] feat(unitproc/bkshuf):Exit early if stdout not being read

---
 unitproc/bkshuf | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/unitproc/bkshuf b/unitproc/bkshuf
index d2d0763..ff80d8e 100644
--- a/unitproc/bkshuf
+++ b/unitproc/bkshuf
@@ -1,10 +1,12 @@
 #!/usr/bin/env bash
 # Desc: Mixes input lines while also preserving some neighbors
 # Usage: cat file | bkshuf arg1
-# Version 0.1.2
+# Version 0.2.0
 # Depends: bc 1.07.1, GNU Coreutils 8.32 (shuf)
 # Input: var: arg1  initial lines to output
 
+trap 'exit;' SIGPIPE; # exit early if stdout not being read
+
 # Load env vars
 ## For these numbers of lines of input...
 if [[ ! -v BKSHUF_PARAM_LINEC ]]; then BKSHUF_PARAM_LINEC=1000000; fi;
@@ -88,7 +90,8 @@ consume_line() {
         line="${list_input[$ip]}";
         if [[ -n $line ]]; then
             ##### consume line at index ip
-            printf "%s\n" "$line" & # print to output
+            printf "%s\n" "$line"; # print to output
+
             unset "list_input[$ip]"; # destroy line in list_input array
             ((lc_out++));
             ((lcr--)); # decrement line count remaining lcr
-- 
2.39.5