X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=appendu;h=7854aee9b1c324de99f907a429e62f465a63e9b8;hb=85a2296c2fd76e0efd4d09807d054b358718e199;hp=f08a45c13a6248d3becbc2e5782e380163cfb469;hpb=91ceba1b2739bb4d21d3c7887c0aff55f6da129d;p=tee-unique diff --git a/appendu b/appendu index f08a45c..7854aee 100755 --- a/appendu +++ b/appendu @@ -3,8 +3,10 @@ # This program is under GPL v. 3 or later, see 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 @@ -28,13 +30,13 @@ Appended lines are output to the terminal. 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 - local readsudo writesudo + local readsudo writesudo x local file="$1" shift @@ -47,15 +49,23 @@ Appended lines are output to the terminal. [[ ! -w $dir ]] && writesudo=sudo else echo "appendu error: $dir does not exist" - exit 1 + return 1 fi fi if ! $dosudo; then 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 "$@"