1d9388e8840b580327d6ae4bc8ff41ef1344602f
2 # Copyright (C) 2014 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
6 local help="Usage: appendu [OPTION]... FILE [LINE]...
8 Append each line to FILE if it does not exist in FILE.
9 Use LINE if specified, else use lines from stdin.
10 Appended lines are output to the terminal.
12 -s don't try to use sudo when it would help us read or write the file
13 -- stop processing arguments
14 --help display this message"
19 if [[ $1 == --help ]]; then
22 elif [[ $1 == -s ]]; then
25 elif [[ $1 == -- ]]; then
33 if [[ ${#@} == 0 ]]; then
34 echo "error: need 1 or more arguments"
39 local readsudo writesudo
43 if [[ -e $file ]]; then
44 [[ -r $file ]] || readsudo
=sudo
45 [[ -w $file ]] || writesudo
=sudo
47 local dir
="$(dirname "$file")"
48 if [[ -d $dir ]]; then
49 [[ ! -w $dir ]] && writesudo
=sudo
51 echo "appendu error: $dir does not exist"
61 [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" ||
$writesudo tee -a "$file"<<<"$x"
63 elif [[ ! -t 0 ]]; then
66 # duplicated from above
67 [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" ||
$writesudo tee -a "$file"<<<"$x"