b613353af65fcd5eef8a3fd0c61491f4997ce6d2
[tee-unique] / appendu
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
4
5 appendu() {
6 local help="Usage: appendu [OPTION]... FILE [LINE_SET]...
7
8 Append unique.
9
10 A LINE_SET is one or more lines. Append LINE_SET to FILE if it does not exist in
11 FILE. If no LINE_SET argument is given, read lines from stdin, and treat each
12 as a single LINE_SET. Appended text is output to the terminal. Duplicate
13 LINE_SETs are treated the same.
14
15 -- stop processing arguments
16 [-h|--help] display this message"
17
18
19 while true; do
20 if [[ $1 == --help || $1 == -h ]]; then
21 echo "$help"
22 return
23 elif [[ $1 == -- ]]; then
24 shift
25 break
26 else
27 break
28 fi
29 done
30
31 if [[ ${#@} == 0 ]]; then
32 echo "error: need 1 or more arguments"
33 echo "$help"
34 return 1
35 fi
36
37 local file="$1"
38 shift
39
40 local new_file=true
41 if [[ -e $file ]]; then
42 new_file=false
43 else
44 local dir="$(dirname "$file")"
45 if [[ ! -d $dir ]]; then
46 echo "appendu error: $dir does not exist"
47 return 1
48 fi
49 fi
50 if ! $new_file; then
51 if [[ ! -r $file ]]; then
52 echo "appendu error: cannot read or write $file"
53 return 1
54 fi
55 if [[ ! -w $file ]]; then
56 echo "appendu error: cannot read or write $file"
57 return 1
58 fi
59 fi
60
61 if (( $# == 0 )); then
62 unset IFS
63 while read -r x; do
64 strings+=( "$x" )
65 done
66 else
67 strings=( "$@" )
68 fi
69
70 # fix files with no newline at the end.
71 # the following command won't work right on them otherwise.
72 # e = run script, $a\ means append following text, but there is none,
73 # so sed only does what it always does when it was supposed to modify a file,
74 # which is append a newline if there was none.
75 sed -ie '$a\' "$file"
76 # command substitution removes any trailing newlines, so we have to add
77 # a non-newline ending, we randomly chose "b", then remove it.
78 content=$(cat "$file"; echo b) content=${content%b}
79
80 # we aren't using regex because we want to match strings,
81 # but we also want our match to start at the beginning of a line,
82 # or the beginning of the file, and to end at a line ending.
83 # So we do some slick bash to match this.
84 local start="?(*
85 )"
86 local end="
87 *"
88 local return_code
89 for string in "${strings[@]}"; do
90 if $new_file || [[ $content != $start"$string"$end ]]; then
91 if ! tee -a "$file"<<<"$string"; then
92 return_code=$?
93 echo "appendu error: error writing to $file"
94 return $return_code
95 fi
96 fi
97 done
98 return 0
99 }
100 appendu "$@"