#!/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 "$@" ); };