From 6a6e2a33966e7e2d4f52fe0f9d03939dc183fc71 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Fri, 4 Apr 2025 11:03:58 +0000 Subject: [PATCH] feat(unitproc/bkt-printl):print lines --- unitproc/bkt-printl | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 unitproc/bkt-printl diff --git a/unitproc/bkt-printl b/unitproc/bkt-printl new file mode 100644 index 0000000..799cabd --- /dev/null +++ b/unitproc/bkt-printl @@ -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 -- 2.39.5