]> zdv2.bktei.com Git - BK-2020-03.git/commitdiff
feat(unitproc/bkt-printl):print lines
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Fri, 4 Apr 2025 11:03:58 +0000 (11:03 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Fri, 4 Apr 2025 11:03:58 +0000 (11:03 +0000)
unitproc/bkt-printl [new file with mode: 0644]

diff --git a/unitproc/bkt-printl b/unitproc/bkt-printl
new file mode 100644 (file)
index 0000000..799cabd
--- /dev/null
@@ -0,0 +1,68 @@
+#!/bin/bash
+
+function printl() {
+    function yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
+    function die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
+    function showUsage() {
+        # Desc: Display script usage information
+        # Usage: showUsage
+        # Input: none
+        # Output: stderr
+        yell "USAGE:";
+        yell "printl [int] [int] ([path])";
+        yell ""
+        yell "EXAMPLE:"
+        yell "printl 5 17 somefile.txt"
+    }; # Display information on how to use this script.
+    function main() {
+        # Desc: Prints ranges of lines
+        # Usage: printl [int] [int] [path]
+        # Version: 0.0.1
+        # Example: printl 5 17 somefile.txt  # prints lines 5-17
+        # Depends: Bash 5.1.16
+        start="$1";
+        end="$2";
+
+        # Require first two args be integers
+        re='^[0-9]+$';
+        if [[ ! "$start" =~ $re ]]; then
+            showUsage;
+            die "FATAL:First argument (start line number) not an integer:$(declare -p start)";
+        fi;
+        if [[ ! "$end" =~ $re ]]; then
+            showUsage;
+            die "FATAL:Second argument (end line number) not an integer:$(declare -p end)";
+        fi;
+
+        # Require between 2 and 3 arguments are provided
+        if [[ $# -ge 4 ]]; then
+            showUsage;
+            die "FATAL:Too many arguments:$#";
+        fi;
+        if [[ $# -le 1 ]]; then
+            showUsage;
+            die "FATAL:Too few arguments:$#";
+        fi;
+
+        # Handle 3 argument case (int int file)
+        if [[ $# -eq 3 ]] && [[ -f "$3" ]]; then
+            # Use specified file
+            input="$3";
+        elif [[ $# -eq 3 ]] && [[ ! -f "$3" ]]; then
+            showUsage;
+            die "FATAL:Third argument not a file.";
+        fi;
+
+        # Handle 2 argument case (int int)
+        if [[ $# -eq 2 ]]; then
+            # Use stdin
+            input="-"
+        fi;
+
+        # Print lines
+        tail -n+${start} "$input" | head -n$((end - start + 1));
+
+    }; # main program
+
+    ( must main "$@" ); 
+};
\ No newline at end of file