Commit | Line | Data |
---|---|---|
966a8d11 SBS |
1 | #!/usr/bin/env bash |
2 | ||
3 | filter_no_dots() { | |
4 | # Desc: Filters stdin lines for paths with basenames that are not dotfiles | |
5 | # Usage: printf "foo\n" | filter_no_dots [path] | |
6 | # Input: stdin (consumes) | |
7 | # args | |
8 | # Output: stdout (only outputs lines if basename is not a dotfile) | |
9 | # Version: 0.0.1 | |
10 | # Depends: GNU bash (v5.1.16) | |
11 | ||
12 | local input_stdin input_psarg buffer re output; | |
13 | ||
14 | # Store stdin | |
15 | if [[ -p /dev/stdin ]]; then | |
16 | input_stdin="$(cat -)"; | |
17 | fi; | |
18 | ||
19 | # Store arguments | |
20 | if [[ $# -gt 0 ]]; then | |
21 | input_psarg="$*"; | |
22 | fi; | |
23 | ||
24 | # Combine as buffer array elements | |
25 | ## Read in stdin | |
26 | if [[ -n $input_stdin ]]; then | |
27 | while read -r line; do | |
28 | buffer+=("$line"); | |
29 | done < <(printf "%s\n" "$input_stdin"); | |
30 | fi; | |
31 | ## Read in positional arguments | |
32 | if [[ -n $input_psarg ]]; then | |
33 | for arg in "$@"; do | |
34 | buffer+=("$arg"); | |
35 | done; | |
36 | fi; | |
37 | ||
38 | # Form output array | |
39 | re="^\."; # first char is a dot | |
40 | while read -r line; do | |
41 | # Get path basename | |
42 | file_name="$(basename "$line")"; | |
43 | # Add path to output if basename not a dotfile | |
44 | if [[ ! "$file_name" =~ $re ]]; then | |
45 | output+=("$line"); | |
46 | else | |
47 | continue; | |
48 | fi; | |
49 | done < <( printf "%s\n" "${buffer[@]}" ); | |
50 | ||
51 | # Print output array to stdout | |
52 | printf "%s\n" "${output[@]}"; | |
53 | }; # Filters stdin lines for paths with basenames that are not dotfiles | |
54 | ||
55 | # Test | |
56 | printf "foo\n.bar\nbaz\n" | filter_no_dots "$HOME/.local" "$HOME/" |