# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
- local help="Usage: appendu [OPTION]... FILE LINE...
-Append unique. Append each LINE to FILE if it does not exist in FILE.
+ local help="Usage: appendu [OPTION]... FILE [LINE]...
+Append unique.
+Append each line to FILE if it does not exist in FILE.
+Use LINE if specified, else use lines from stdin.
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
fi
done
- if (( ${#@} < 2 )); then
- echo "error: need 2 or more arguments"
+ if [[ ${#@} == 0 ]]; then
+ echo "error: need 1 or more arguments"
echo "$help"
return 1
fi
readsudo=
writesudo=
fi
- for x in "$@"; do
- [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
- done
+ if [[ $@ ]]; then
+ for x in "$@"; do
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ elif [[ ! -t 0 ]]; then
+ unset IFS
+ while read -r x; do
+ # duplicated from above
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ fi
}
appendu "$@"
# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
- local help="Usage: appendu [OPTION]... FILE LINE...
-Append unique. Append each LINE to FILE if it does not exist in FILE.
+ local help="Usage: appendu [OPTION]... FILE [LINE]...
+Append unique.
+Append each line to FILE if it does not exist in FILE.
+Use LINE if specified, else use lines from stdin.
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
fi
done
- if (( ${#@} < 2 )); then
- echo "error: need 2 or more arguments"
+ if [[ ${#@} == 0 ]]; then
+ echo "error: need 1 or more arguments"
echo "$help"
return 1
fi
readsudo=
writesudo=
fi
- for x in "$@"; do
- [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
- done
+ if [[ $@ ]]; then
+ for x in "$@"; do
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ elif [[ ! -t 0 ]]; then
+ unset IFS
+ while read -r x; do
+ # duplicated from above
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ fi
}