File: stacks-dist-extract

package info (click to toggle)
stacks 2.55%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,420 kB
  • sloc: cpp: 38,596; perl: 1,337; sh: 539; python: 497; makefile: 144
file content (29 lines) | stat: -rwxr-xr-x 773 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash

usage="\
Usage:
  $(basename "$0") DIST_FILE
  $(basename "$0") DIST_FILE SECTION_NAME

(1) Prints available sections/distributions for DIST_FILE.
(2) Extracts section SECTION_NAME from DIST_FILE.
"

if { [[ $# -ne 1 ]] && [[ $# -ne 2 ]]; } || [[ "$1" =~ ^(-h|--help|--version)$ ]] ;then
    echo -n "$usage" >&2
    exit 1
fi

dist_f="$1"
[[ -e "$dist_f" ]] || { ls -- "$dist_f"; exit 1; }

if [[ "$2" ]] ;then
    section="$(echo "$2" | grep -oE '\w+' | tr -d '\n')"
    if ! grep --quiet "^BEGIN $section\\b" "$dist_f" ;then
        echo "Error: Couldn't find section '$section' in '$dist_f'." >&1
        exit 1
    fi
    sed -nE "/^END $section($| )/ q; /^BEGIN $section($| )/,$ p" "$dist_f" | sed 1d
else
    grep ^BEGIN "$dist_f" | cut -c7-
fi