]> zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkt-printl
feat(unitproc/bkt-printl):print lines
[BK-2020-03.git] / unitproc / bkt-printl
1 #!/bin/bash
2
3 function printl() {
4 function yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
5 function die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
6 function showUsage() {
7 # Desc: Display script usage information
8 # Usage: showUsage
9 # Input: none
10 # Output: stderr
11 yell "USAGE:";
12 yell "printl [int] [int] ([path])";
13 yell ""
14 yell "EXAMPLE:"
15 yell "printl 5 17 somefile.txt"
16 }; # Display information on how to use this script.
17 function main() {
18 # Desc: Prints ranges of lines
19 # Usage: printl [int] [int] [path]
20 # Version: 0.0.1
21 # Example: printl 5 17 somefile.txt # prints lines 5-17
22 # Depends: Bash 5.1.16
23 start="$1";
24 end="$2";
25
26 # Require first two args be integers
27 re='^[0-9]+$';
28 if [[ ! "$start" =~ $re ]]; then
29 showUsage;
30 die "FATAL:First argument (start line number) not an integer:$(declare -p start)";
31 fi;
32 if [[ ! "$end" =~ $re ]]; then
33 showUsage;
34 die "FATAL:Second argument (end line number) not an integer:$(declare -p end)";
35 fi;
36
37 # Require between 2 and 3 arguments are provided
38 if [[ $# -ge 4 ]]; then
39 showUsage;
40 die "FATAL:Too many arguments:$#";
41 fi;
42 if [[ $# -le 1 ]]; then
43 showUsage;
44 die "FATAL:Too few arguments:$#";
45 fi;
46
47 # Handle 3 argument case (int int file)
48 if [[ $# -eq 3 ]] && [[ -f "$3" ]]; then
49 # Use specified file
50 input="$3";
51 elif [[ $# -eq 3 ]] && [[ ! -f "$3" ]]; then
52 showUsage;
53 die "FATAL:Third argument not a file.";
54 fi;
55
56 # Handle 2 argument case (int int)
57 if [[ $# -eq 2 ]]; then
58 # Use stdin
59 input="-"
60 fi;
61
62 # Print lines
63 tail -n+${start} "$input" | head -n$((end - start + 1));
64
65 }; # main program
66
67 ( must main "$@" );
68 };