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...
7 Append unique. Append each LINE to FILE if it does not exist in FILE.
8 Appended lines are output to the terminal.
10 -s don't try to use sudo when it would help us read or write the file
11 -- stop processing arguments
12 --help display this message"
17 if [[ $1 == --help ]]; then
20 elif [[ $1 == -s ]]; then
23 elif [[ $1 == -- ]]; then
31 if (( ${#@} < 2 )); then
32 echo "error: need 2 or more arguments"
37 local readsudo writesudo
41 if [[ -e $file ]]; then
42 [[ -r $file ]] || readsudo
=sudo
43 [[ -w $file ]] || writesudo
=sudo
45 local dir
="$(dirname "$file")"
46 if [[ -d $dir ]]; then
47 [[ ! -w $dir ]] && writesudo
=sudo
49 echo "appendu error: $dir does not exist"
58 [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" ||
$writesudo tee -a "$file"<<<"$x"