feat(unitproc/bkt-get_median):Add Bash function to calculate median
[BK-2020-03.git] / unitproc / bkgettext
1 #!/bin/bash
2
3 # Date: 2020-02-15T22:23Z
4
5 # Author: Steven Baltakatei Sandoval
6
7 # Description: `bkgettext` prints text between and including two
8 # specified tags.
9
10 # Usage:
11 # $ bkgettext [ -s tagStart ] [ -e tagEnd ] [ -f file ]
12
13 echoerr() { echo "$@" 1>&2; } # function for outputing to stderr; see [3]
14 vc() { [ "$OPTION_VERBOSE" == "true" ] && return 0 || return 1; } # returns true if verbose mode active
15 print_help() { echoerr "Usage: [ -s tagStart ] [ -e tagEnd ] [ -f file ] "; } # print help
16 process_inputs() {
17 # echoerr "process_inputs() \$1:""===""$1""==="
18 # echoerr "process_inputs() \$2:""===""$2""==="
19 # echoerr "process_inputs() \$3:""===""$3""==="
20 # echoerr "process_inputs() \$4:""===""$4""==="
21 # echoerr "process_inputs() \$5:""===""$5""==="
22 # echoerr "process_inputs() \$6:""===""$6""==="
23 # echoerr "process_inputs() \$7:""===""$7""==="
24 # echoerr "process_inputs() \$8:""===""$8""==="
25
26 while getopts "vhs:e:f:" options_array; do
27 case "${options_array}" in
28 v)
29 OPTION_VERBOSE="true"
30 vc && echoerr "DEBUG:Verbose mode active."
31 ;;
32 h)
33 print_help; exit 1
34 ;;
35 s)
36 tagStart=${OPTARG}
37 vc && echoerr "DEBUG:tagStart:""$tagStart"
38 ;;
39 e)
40 tagEnd=${OPTARG}
41 vc && echoerr "DEBUG:tagEnd:""$tagEnd"
42 ;;
43 f)
44 [ ! -f ${OPTARG} ] && echoerr "ERROR: Invalid file name provided." && exit 1;
45 fileInput=${OPTARG}
46 vc && echoerr "DEBUG:fileInput:""$fileInput"
47 ;;
48 *)
49 echoerr "Error."
50 ;;
51 esac
52 done
53 shift $((OPTIND-1))
54 }
55
56 main() {
57
58 process_inputs "$@" # Define variables: tagStart tagEnd fileInput
59 # echoerr "main() \$1:""===""$1""==="
60 # echoerr "main() \$2:""===""$2""==="
61 # echoerr "main() \$3:""===""$3""==="
62 # echoerr "main() \$4:""===""$4""==="
63 # echoerr "main() \$5:""===""$5""==="
64 # echoerr "main() \$6:""===""$6""==="
65 # echoerr "main() \$7:""===""$7""==="
66 # echoerr "main() \$8:""===""$8""==="
67 # echoerr "main() \$tagStart:""===""$tagStart""==="
68 # echoerr "main() \$tagEnd:""===""$tagEnd""==="
69 # echoerr "main() \$fileInput:""===""$fileInput""==="
70 #OUTPUT=$(cat "$fileInput" | awk "/$tagStart/,/$tagEnd/") # get text between and including tag lines; see [1]
71 #OUTPUT=$(cat "$fileInput" | awk "/$tagStart/{f=1;next} /$tagEnd/{f=0} f") # get text between tag lines; see [1]
72 OUTPUT=$(cat "$fileInput" | awk "/$tagStart/{f=1} /$tagEnd/{f=0;print} f") # get text between and including tag lines; see [1]
73 echo "$OUTPUT"
74 }
75
76 main "$@" # pass arguments to function "main" and execute "main" (for why `"$@"`, see [2])
77 exit 0 # exit normally
78
79
80
81 #===References===
82 # [1]: How to get text between and including specified strings: https://stackoverflow.com/a/22222219
83 # [2]: How to correctly pass bash script arguments to functions: https://stackoverflow.com/a/8198970
84 # [3]: How to print text to stderr instead of stdout: https://stackoverflow.com/a/2990533